<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge"><![endif]-->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="Asciidoctor 1.5.7.1">
<title>Flowable CMMN 用户手册 (v 6.4.2-SNAPSHOT)</title>
<style>

/* normalize.css */

button,hr,input{overflow:visible}audio,canvas,progress,video{display:inline-block}progress,sub,sup{vertical-align:baseline}html{font-family:sans-serif;line-height:1.15;-ms-text-size-adjust:100%;-webkit-text-size-adjust:100%}body{margin:0} menu,article,aside,details,footer,header,nav,section{display:block}h1{font-size:2em;margin:.67em 0}figcaption,figure,main{display:block}figure{margin:1em 40px}hr{box-sizing:content-box;height:0}code,kbd,pre,samp{font-family:monospace,monospace;font-size:1em}a{background-color:transparent;-webkit-text-decoration-skip:objects}a:active,a:hover{outline-width:0}abbr[title]{border-bottom:none;text-decoration:underline;text-decoration:underline dotted}b,strong{font-weight:bolder}dfn{font-style:italic}mark{background-color:#ff0;color:#000}small{font-size:80%}sub,sup{font-size:75%;line-height:0;position:relative}sub{bottom:-.25em}sup{top:-.5em}audio:not([controls]){display:none;height:0}img{border-style:none}svg:not(:root){overflow:hidden}button,input,optgroup,select,textarea{font-family:sans-serif;font-size:100%;line-height:1.15;margin:0}button,input{}button,select{text-transform:none}[type=submit], [type=reset],button,html [type=button]{-webkit-appearance:button}[type=button]::-moz-focus-inner,[type=reset]::-moz-focus-inner,[type=submit]::-moz-focus-inner,button::-moz-focus-inner{border-style:none;padding:0}[type=button]:-moz-focusring,[type=reset]:-moz-focusring,[type=submit]:-moz-focusring,button:-moz-focusring{outline:ButtonText dotted 1px}fieldset{border:1px solid silver;margin:0 2px;padding:.35em .625em .75em}legend{box-sizing:border-box;color:inherit;display:table;max-width:100%;padding:0;white-space:normal}progress{}textarea{overflow:auto}[type=checkbox],[type=radio]{box-sizing:border-box;padding:0}[type=number]::-webkit-inner-spin-button,[type=number]::-webkit-outer-spin-button{height:auto}[type=search]{-webkit-appearance:textfield;outline-offset:-2px}[type=search]::-webkit-search-cancel-button,[type=search]::-webkit-search-decoration{-webkit-appearance:none}::-webkit-file-upload-button{-webkit-appearance:button;font:inherit}summary{display:list-item}[hidden],template{display:none}/*# sourceMappingURL=normalize.min.css.map */

/* tocbot 2.1.4 */

.toc{overflow-y:auto}.toc>ul{overflow:hidden;position:relative}.toc-list{margin:0;padding-left:10px}a.toc-link{color:currentColor;height:100%}.is-collapsible{max-height:1000px;overflow:hidden;transition:all 300ms ease-in-out}.is-collapsed{max-height:0}.is-position-fixed{position:fixed !important;top:0}.is-active-link{font-weight:700}.toc-link::before{background-color:#f9f9fb;content:' ';display:inline-block;height:inherit;left:8px;margin-top:-1px;position:absolute;width:2px}.is-active-link::before{background-color:#54BC4B}


/* Activiti Specific */

body {
  color: #494d55;
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

html, body {
  height: 100%;
  width: 100%;
  background: #f9f9f9;
  font-family: "Helvetica Neue",Helvetica,Roboto,Arial,sans-serif;
  color: #484848
}

.page-wrapper {
  min-height: 100%;
  /* equal to footer height */
  margin-bottom: -50px;
}

.page-wrapper:after {
  content: "";
  display: block;
  height: 50px;
}

#header {
  position:fixed;
  top:0px;
  z-index:100000;
  width:100%;
}

#header > h1 {
    background: #a0cc47;
    color: #f9f9f9;
    border-bottom: 2px solid #042944;
    padding: 20px 0 20px 20px;
    font-size: 40px;
    font-weight: 800;
    line-height: 1.1;
    margin: 0;
}

#header > h1:before {
  background:url('images/logo.png');
  background-size: 337px 54px; background-repeat: no-repeat;
  width: 337px;
  height: 54px;
  display:inline-block;
  content:'';
  margin-right: 30px;
}

#author {
  margin-left: 25%;
  font-size: 40px;
}

#content {
  margin: 129px 0 0 27%;
  padding: 10px 40px 0 20px;
  padding-right: 40px;
  line-height: 1.7;
  width: 65%;
  overflow-x: auto;
}

h2, h3, h4, h5, h6 {
  color: #00345a;
}

h2 {
  font-size: 26px;
  margin-top: 0;
  margin-bottom: 20px;
  font-weight: bold;
  padding-bottom: 10px;
  border-bottom: 1px solid #d7d7d7;
}

h3 {
  font-size: 18px;
  margin-top: 0;
  margin-bottom: 0;
  font-weight: bold;
  padding-bottom: 10px;
  /*border-bottom: 1px solid #d7d7d7;*/
}

h4 {
    font-size: 16px;
    font-weight: bold;
}

h5 {
    font-size: 14px;
    font-weight: bold;
}

h6, h7 {
    font-size: 14px;
    font-weight: bold;
    margin-top: 0;
    margin-bottom: 0;
}

.sect1, .sect2 {
  margin-bottom: 20px;
}

.clear
{
  clear: both;
}

.note, .listingblock, .literalblock{
  padding-left: 8px;
  padding-right: 8px;
  margin-bottom: 14px;
  background-color: #f7f7f9;
  border: 1px solid #e1e1e8;
  border-radius: 4px;
  color: #808080;
}

table.tableblock {
  width: 100%;
  max-width: 100%;
  margin-bottom: 20px;
  background-color: transparent;
  border-spacing: 0;
  border-collapse: collapse;
  border: 1px solid #ddd;
  table-layout: fixed;
}

table.tableblock>caption+thead>tr:first-child>td, table.tableblock>caption+thead>tr:first-child>th, table.tableblock>colgroup+thead>tr:first-child>td, table.tableblock>colgroup+thead>tr:first-child>th, table.tableblock>thead:first-child>tr:first-child>td, table.tableblock>thead:first-child>tr:first-child>th {
    border-top: 0;
}

table.tableblock > thead > tr > th {
    border-bottom-color: #8bd6d8;
}

table.tableblock>thead>tr>th {
    vertical-align: bottom;
    border-bottom: 1px solid #ddd;
}

table.tableblock>tbody>tr>td, table.tableblock>tbody>tr>th, table.tableblock>tfoot>tr>td, table.tableblock>tfoot>tr>th, table.tableblock>thead>tr>td, table.tableblock>thead>tr>th {
    padding: 8px;
    line-height: 1.42857143;
    vertical-align: top;
    border-top: 1px solid #ddd;
}

th {
    text-align: left;
}

table.tableblock > tbody > tr:nth-of-type(odd) {
    background-color: #f5f5f5;
}

table.tableblock > tbody > tr:nth-of-type(odd) {
    background-color: #f9f9f9;
}

td > p {
  margin:0;
}

td.content {
  white-space: normal;
}

pre, td, th {
   white-space: pre-wrap;
   white-space: -moz-pre-wrap;
   white-space: -pre-wrap;
   white-space: -o-pre-wrap;
   word-wrap: break-word;
}

img {
  max-width: 100%;
}

a {
  color: #007387;
}

a:hover {
  color: #004c59;
}

#toc-parent {
  width: 25%;
  float: left;
  height: 100%;
  overflow-y: auto;
  position: fixed;
  padding: 10px;
  border-right: 1px solid #ccc;
}

#generated-toc {
    width: 100%;
    height: 100%;
    font-size: 16px;
}

.toc-link {
  text-decoration: none;
}

.toc-list {
  margin: 0 20px 0 10px;
}

.toc-list-item {
  padding: 4px 0 4px 0;
}


div.sect5  p {
    margin-top: 0;
    margin-bottom: 0;
}

div.sect5  code {
    font-family: "Helvetica Neue",Helvetica,Roboto,Arial,sans-serif;
}
</style>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
.listingblock .pygments .hll { background-color: #ffffcc }
.listingblock .pygments, .listingblock .pygments code { background: #f8f8f8; }
.listingblock .pygments .tok-c { color: #408080; font-style: italic } /* Comment */
.listingblock .pygments .tok-err { border: 1px solid #FF0000 } /* Error */
.listingblock .pygments .tok-k { color: #008000; font-weight: bold } /* Keyword */
.listingblock .pygments .tok-o { color: #666666 } /* Operator */
.listingblock .pygments .tok-ch { color: #408080; font-style: italic } /* Comment.Hashbang */
.listingblock .pygments .tok-cm { color: #408080; font-style: italic } /* Comment.Multiline */
.listingblock .pygments .tok-cp { color: #BC7A00 } /* Comment.Preproc */
.listingblock .pygments .tok-cpf { color: #408080; font-style: italic } /* Comment.PreprocFile */
.listingblock .pygments .tok-c1 { color: #408080; font-style: italic } /* Comment.Single */
.listingblock .pygments .tok-cs { color: #408080; font-style: italic } /* Comment.Special */
.listingblock .pygments .tok-gd { color: #A00000 } /* Generic.Deleted */
.listingblock .pygments .tok-ge { font-style: italic } /* Generic.Emph */
.listingblock .pygments .tok-gr { color: #FF0000 } /* Generic.Error */
.listingblock .pygments .tok-gh { color: #000080; font-weight: bold } /* Generic.Heading */
.listingblock .pygments .tok-gi { color: #00A000 } /* Generic.Inserted */
.listingblock .pygments .tok-go { color: #888888 } /* Generic.Output */
.listingblock .pygments .tok-gp { color: #000080; font-weight: bold } /* Generic.Prompt */
.listingblock .pygments .tok-gs { font-weight: bold } /* Generic.Strong */
.listingblock .pygments .tok-gu { color: #800080; font-weight: bold } /* Generic.Subheading */
.listingblock .pygments .tok-gt { color: #0044DD } /* Generic.Traceback */
.listingblock .pygments .tok-kc { color: #008000; font-weight: bold } /* Keyword.Constant */
.listingblock .pygments .tok-kd { color: #008000; font-weight: bold } /* Keyword.Declaration */
.listingblock .pygments .tok-kn { color: #008000; font-weight: bold } /* Keyword.Namespace */
.listingblock .pygments .tok-kp { color: #008000 } /* Keyword.Pseudo */
.listingblock .pygments .tok-kr { color: #008000; font-weight: bold } /* Keyword.Reserved */
.listingblock .pygments .tok-kt { color: #B00040 } /* Keyword.Type */
.listingblock .pygments .tok-m { color: #666666 } /* Literal.Number */
.listingblock .pygments .tok-s { color: #BA2121 } /* Literal.String */
.listingblock .pygments .tok-na { color: #7D9029 } /* Name.Attribute */
.listingblock .pygments .tok-nb { color: #008000 } /* Name.Builtin */
.listingblock .pygments .tok-nc { color: #0000FF; font-weight: bold } /* Name.Class */
.listingblock .pygments .tok-no { color: #880000 } /* Name.Constant */
.listingblock .pygments .tok-nd { color: #AA22FF } /* Name.Decorator */
.listingblock .pygments .tok-ni { color: #999999; font-weight: bold } /* Name.Entity */
.listingblock .pygments .tok-ne { color: #D2413A; font-weight: bold } /* Name.Exception */
.listingblock .pygments .tok-nf { color: #0000FF } /* Name.Function */
.listingblock .pygments .tok-nl { color: #A0A000 } /* Name.Label */
.listingblock .pygments .tok-nn { color: #0000FF; font-weight: bold } /* Name.Namespace */
.listingblock .pygments .tok-nt { color: #008000; font-weight: bold } /* Name.Tag */
.listingblock .pygments .tok-nv { color: #19177C } /* Name.Variable */
.listingblock .pygments .tok-ow { color: #AA22FF; font-weight: bold } /* Operator.Word */
.listingblock .pygments .tok-w { color: #bbbbbb } /* Text.Whitespace */
.listingblock .pygments .tok-mb { color: #666666 } /* Literal.Number.Bin */
.listingblock .pygments .tok-mf { color: #666666 } /* Literal.Number.Float */
.listingblock .pygments .tok-mh { color: #666666 } /* Literal.Number.Hex */
.listingblock .pygments .tok-mi { color: #666666 } /* Literal.Number.Integer */
.listingblock .pygments .tok-mo { color: #666666 } /* Literal.Number.Oct */
.listingblock .pygments .tok-sa { color: #BA2121 } /* Literal.String.Affix */
.listingblock .pygments .tok-sb { color: #BA2121 } /* Literal.String.Backtick */
.listingblock .pygments .tok-sc { color: #BA2121 } /* Literal.String.Char */
.listingblock .pygments .tok-dl { color: #BA2121 } /* Literal.String.Delimiter */
.listingblock .pygments .tok-sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */
.listingblock .pygments .tok-s2 { color: #BA2121 } /* Literal.String.Double */
.listingblock .pygments .tok-se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */
.listingblock .pygments .tok-sh { color: #BA2121 } /* Literal.String.Heredoc */
.listingblock .pygments .tok-si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */
.listingblock .pygments .tok-sx { color: #008000 } /* Literal.String.Other */
.listingblock .pygments .tok-sr { color: #BB6688 } /* Literal.String.Regex */
.listingblock .pygments .tok-s1 { color: #BA2121 } /* Literal.String.Single */
.listingblock .pygments .tok-ss { color: #19177C } /* Literal.String.Symbol */
.listingblock .pygments .tok-bp { color: #008000 } /* Name.Builtin.Pseudo */
.listingblock .pygments .tok-fm { color: #0000FF } /* Name.Function.Magic */
.listingblock .pygments .tok-vc { color: #19177C } /* Name.Variable.Class */
.listingblock .pygments .tok-vg { color: #19177C } /* Name.Variable.Global */
.listingblock .pygments .tok-vi { color: #19177C } /* Name.Variable.Instance */
.listingblock .pygments .tok-vm { color: #19177C } /* Name.Variable.Magic */
.listingblock .pygments .tok-il { color: #666666 } /* Literal.Number.Integer.Long */
</style>
<script type="text/javascript">

/*
 * Tocbot.min.js version 2.1.4
 */

 !function(e){function t(o){if(n[o])return n[o].exports;var i=n[o]={exports:{},id:o,loaded:!1};return e[o].call(i.exports,i,i.exports,t),i.loaded=!0,i.exports}var n={};return t.m=e,t.c=n,t.p="",t(0)}([/*!*************************!*\
   !*** ./src/js/index.js ***!
   \*************************/
 function(e,t,n){var o,i,r;(function(l){!function(n,l){i=[],o=l(n),r="function"==typeof o?o.apply(t,i):o,!(void 0!==r&&(e.exports=r))}("undefined"!=typeof l?l:this.window||this.global,function(e){"use strict";function t(){for(var e={},t=0;t<arguments.length;t++){var n=arguments[t];for(var o in n)h.call(n,o)&&(e[o]=n[o])}return e}function o(e,t,n){t||(t=250);var o,i;return function(){var r=n||this,l=+new Date,s=arguments;o&&l<o+t?(clearTimeout(i),i=setTimeout(function(){o=l,e.apply(r,s)},t)):(o=l,e.apply(r,s))}}var i,r,l,s=n(/*! smooth-scroll */1),c=n(/*! ./default-options.js */2),a={},u={},d=n(/*! ./build-html.js */3),f=n(/*! ./parse-content.js */4),m=(e.document,document.body,!!e.document.querySelector&&!!e.addEventListener),h=Object.prototype.hasOwnProperty;return u.destroy=function(){try{document.querySelector(a.tocSelector).innerHTML=""}catch(e){console.warn("Element not found: "+a.tocSelector)}document.removeEventListener("scroll",this._scrollListener,!1),document.removeEventListener("resize",this._scrollListener,!1),i&&document.removeEventListener("click",this._clickListener,!1),s&&s.destroy()},u.init=function(e){if(m&&(a=t(c,e||{}),this.options=a,this.state={},i=d(a),r=f(a),this._buildHtml=i,this._parseContent=r,u.destroy(),l=r.selectHeadings(a.contentSelector,a.headingSelector),null!==l)){var n=r.nestHeadingsArray(l),h=n.nest;return i.render(a.tocSelector,h),this._scrollListener=o(function(){i.updateToc(l)},a.throttleTimeout),this._scrollListener(),document.addEventListener("scroll",this._scrollListener,!1),document.addEventListener("resize",this._scrollListener,!1),this._clickListener=o(function(e){i.disableTocAnimation(e),i.updateToc(l)},a.throttleTimeout),document.addEventListener("click",this._clickListener,!1),s&&(this.smoothScroll=s.init(t(a.smoothScrollOptions,{callback:i.enableTocAnimation}))),this}},u.refresh=function(e){u.destroy(),u.init(e||this.options)},e.tocbot=u,u})}).call(t,function(){return this}())},/*!******************************************************!*\
   !*** ./~/smooth-scroll/dist/js/smooth-scroll.min.js ***!
   \******************************************************/
 function(e,t,n){var o,i,r;(function(n){/*! smooth-scroll v10.0.1 | (c) 2016 Chris Ferdinandi | MIT License | http://github.com/cferdinandi/smooth-scroll */
 !function(n,l){i=[],o=l(n),r="function"==typeof o?o.apply(t,i):o,!(void 0!==r&&(e.exports=r))}("undefined"!=typeof n?n:this.window||this.global,function(e){"use strict";var t,n,o,i,r,l,s,c={},a="querySelector"in document&&"addEventListener"in e,u={selector:"[data-scroll]",selectorHeader:null,speed:500,easing:"easeInOutCubic",offset:0,callback:function(){}},d=function(){var e={},t=!1,n=0,o=arguments.length;"[object Boolean]"===Object.prototype.toString.call(arguments[0])&&(t=arguments[0],n++);for(var i=function(n){for(var o in n)Object.prototype.hasOwnProperty.call(n,o)&&(t&&"[object Object]"===Object.prototype.toString.call(n[o])?e[o]=d(!0,e[o],n[o]):e[o]=n[o])};o>n;n++){var r=arguments[n];i(r)}return e},f=function(e){return Math.max(e.scrollHeight,e.offsetHeight,e.clientHeight)},m=function(e,t){var n,o,i=t.charAt(0),r="classList"in document.documentElement;for("["===i&&(t=t.substr(1,t.length-2),n=t.split("="),n.length>1&&(o=!0,n[1]=n[1].replace(/"/g,"").replace(/'/g,"")));e&&e!==document&&1===e.nodeType;e=e.parentNode){if("."===i)if(r){if(e.classList.contains(t.substr(1)))return e}else if(new RegExp("(^|\\s)"+t.substr(1)+"(\\s|$)").test(e.className))return e;if("#"===i&&e.id===t.substr(1))return e;if("["===i&&e.hasAttribute(n[0])){if(!o)return e;if(e.getAttribute(n[0])===n[1])return e}if(e.tagName.toLowerCase()===t)return e}return null},h=function(e){"#"===e.charAt(0)&&(e=e.substr(1));for(var t,n=String(e),o=n.length,i=-1,r="",l=n.charCodeAt(0);++i<o;){if(t=n.charCodeAt(i),0===t)throw new InvalidCharacterError("Invalid character: the input contains U+0000.");r+=t>=1&&31>=t||127==t||0===i&&t>=48&&57>=t||1===i&&t>=48&&57>=t&&45===l?"\\"+t.toString(16)+" ":t>=128||45===t||95===t||t>=48&&57>=t||t>=65&&90>=t||t>=97&&122>=t?n.charAt(i):"\\"+n.charAt(i)}return"#"+r},p=function(e,t){var n;return"easeInQuad"===e&&(n=t*t),"easeOutQuad"===e&&(n=t*(2-t)),"easeInOutQuad"===e&&(n=.5>t?2*t*t:-1+(4-2*t)*t),"easeInCubic"===e&&(n=t*t*t),"easeOutCubic"===e&&(n=--t*t*t+1),"easeInOutCubic"===e&&(n=.5>t?4*t*t*t:(t-1)*(2*t-2)*(2*t-2)+1),"easeInQuart"===e&&(n=t*t*t*t),"easeOutQuart"===e&&(n=1- --t*t*t*t),"easeInOutQuart"===e&&(n=.5>t?8*t*t*t*t:1-8*--t*t*t*t),"easeInQuint"===e&&(n=t*t*t*t*t),"easeOutQuint"===e&&(n=1+--t*t*t*t*t),"easeInOutQuint"===e&&(n=.5>t?16*t*t*t*t*t:1+16*--t*t*t*t*t),n||t},v=function(e,t,n){var o=0;if(e.offsetParent)do o+=e.offsetTop,e=e.offsetParent;while(e);return o=Math.max(o-t-n,0),Math.min(o,b()-g())},g=function(){return Math.max(document.documentElement.clientHeight,e.innerHeight||0)},b=function(){return Math.max(document.body.scrollHeight,document.documentElement.scrollHeight,document.body.offsetHeight,document.documentElement.offsetHeight,document.body.clientHeight,document.documentElement.clientHeight)},C=function(e){return e&&"object"==typeof JSON&&"function"==typeof JSON.parse?JSON.parse(e):{}},S=function(e){return e?f(e)+e.offsetTop:0},y=function(t,n,o){o||(t.focus(),document.activeElement.id!==t.id&&(t.setAttribute("tabindex","-1"),t.focus(),t.style.outline="none"),e.scrollTo(0,n))};c.animateScroll=function(n,o,l){var c=C(o?o.getAttribute("data-options"):null),a=d(t||u,l||{},c),f="[object Number]"===Object.prototype.toString.call(n),m=f||!n.tagName?null:n;if(f||m){var h=e.pageYOffset;a.selectorHeader&&!i&&(i=document.querySelector(a.selectorHeader)),r||(r=S(i));var g,x,N=f?n:v(m,r,parseInt(a.offset,10)),L=N-h,O=b(),E=0,A=function(t,i,r){var l=e.pageYOffset;(t==i||l==i||e.innerHeight+l>=O)&&(clearInterval(r),y(n,i,f),a.callback(n,o))},H=function(){E+=16,g=E/parseInt(a.speed,10),g=g>1?1:g,x=h+L*p(a.easing,g),e.scrollTo(0,Math.floor(x)),A(x,N,s)},T=function(){clearInterval(s),s=setInterval(H,16)};0===e.pageYOffset&&e.scrollTo(0,0),T()}};var x=function(t){e.location.hash,n&&(n.id=n.getAttribute("data-scroll-id"),c.animateScroll(n,o),n=null,o=null)},N=function(i){if(0===i.button&&!i.metaKey&&!i.ctrlKey&&(o=m(i.target,t.selector),o&&"a"===o.tagName.toLowerCase()&&o.hostname===e.location.hostname&&o.pathname===e.location.pathname&&/#/.test(o.href))){var r=h(o.hash);if("#"===r){i.preventDefault(),n=document.body;var l=n.id?n.id:"smooth-scroll-top";return n.setAttribute("data-scroll-id",l),n.id="",void(e.location.hash.substring(1)===l?x():e.location.hash=l)}n=document.querySelector(r),n&&(n.setAttribute("data-scroll-id",n.id),n.id="",o.hash===e.location.hash&&(i.preventDefault(),x()))}},L=function(e){l||(l=setTimeout(function(){l=null,r=S(i)},66))};return c.destroy=function(){t&&(document.removeEventListener("click",N,!1),e.removeEventListener("resize",L,!1),t=null,n=null,o=null,i=null,r=null,l=null,s=null)},c.init=function(n){a&&(c.destroy(),t=d(u,n||{}),i=t.selectorHeader?document.querySelector(t.selectorHeader):null,r=S(i),document.addEventListener("click",N,!1),e.addEventListener("hashchange",x,!1),i&&e.addEventListener("resize",L,!1))},c})}).call(t,function(){return this}())},/*!***********************************!*\
   !*** ./src/js/default-options.js ***!
   \***********************************/
 function(e,t){e.exports={tocSelector:".js-toc",contentSelector:".js-toc-content",headingSelector:"h1, h2, h3",ignoreSelector:".js-toc-ignore",linkClass:"toc-link",extraLinkClasses:"",activeLinkClass:"is-active-link",listClass:"toc-list",extraListClasses:"",isCollapsedClass:"is-collapsed",collapsibleClass:"is-collapsible",listItemClass:"toc-list-item",collapseDepth:0,smoothScrollOptions:{easing:"easeInOutCubic",offset:0,speed:300,updateURL:!0},headingsOffset:0,throttleTimeout:50,positionFixedSelector:null,positionFixedClass:"is-position-fixed",fixedSidebarOffset:"auto",includeHtml:!1}},/*!******************************!*\
   !*** ./src/js/build-html.js ***!
   \******************************/
 function(e,t){e.exports=function(e){function t(e,n){var r=n.appendChild(o(e));if(e.children.length){var l=i(e.isCollapsed);e.children.forEach(function(e){t(e,l)}),r.appendChild(l)}}function n(e,n){var o=!1,r=i(o);n.forEach(function(e){t(e,r)});var l=document.querySelector(e);if(null!==l)return l.firstChild&&l.removeChild(l.firstChild),l.appendChild(r)}function o(t){var n=document.createElement("li"),o=document.createElement("a");return e.listItemClass&&n.setAttribute("class",e.listItemClass),e.includeHtml&&t.childNodes.length?u.call(t.childNodes,function(e){o.appendChild(e.cloneNode(!0))}):o.textContent=t.textContent,o.setAttribute("data-scroll",""),o.setAttribute("href","#"+t.id),o.setAttribute("class",e.linkClass+h+"node-name--"+t.nodeName+h+e.extraLinkClasses),n.appendChild(o),n}function i(t){var n=document.createElement("ul"),o=e.listClass+h+e.extraListClasses;return t&&(o+=h+e.collapsibleClass,o+=h+e.isCollapsedClass),n.setAttribute("class",o),n}function r(){var t=document.documentElement.scrollTop||f.scrollTop,n=document.querySelector(e.positionFixedSelector);"auto"===e.fixedSidebarOffset&&(e.fixedSidebarOffset=document.querySelector(e.tocSelector).offsetTop),t>e.fixedSidebarOffset?n.className.indexOf(e.positionFixedClass)===-1&&(n.className+=h+e.positionFixedClass):n.className=n.className.split(h+e.positionFixedClass).join("")}function l(t){var n=document.documentElement.scrollTop||f.scrollTop;e.positionFixedSelector&&r();var o,i=t;if(m&&null!==document.querySelector(e.tocSelector)&&i.length>0){d.call(i,function(t,r){if(t.offsetTop>n+e.headingsOffset){var l=0===r?r:r-1;return o=i[l],!0}if(r===i.length-1)return o=i[i.length-1],!0});var l=document.querySelector(e.tocSelector).querySelectorAll("."+e.linkClass);u.call(l,function(t){t.className=t.className.split(h+e.activeLinkClass).join("")});var c=document.querySelector(e.tocSelector).querySelector("."+e.linkClass+".node-name--"+o.nodeName+'[href="#'+o.id+'"]');c.className+=h+e.activeLinkClass;var a=document.querySelector(e.tocSelector).querySelectorAll("."+e.listClass+"."+e.collapsibleClass);u.call(a,function(t){var n=h+e.isCollapsedClass;t.className.indexOf(n)===-1&&(t.className+=h+e.isCollapsedClass)}),c.nextSibling&&(c.nextSibling.className=c.nextSibling.className.split(h+e.isCollapsedClass).join("")),s(c.parentNode.parentNode)}}function s(t){return t.className.indexOf(e.collapsibleClass)!==-1?(t.className=t.className.split(h+e.isCollapsedClass).join(""),s(t.parentNode.parentNode)):t}function c(t){var n=t.target||t.srcElement;n.className.indexOf(e.linkClass)!==-1&&(m=!1)}function a(){m=!0}var u=[].forEach,d=[].some,f=document.body,m=!0,h=" ";return{enableTocAnimation:a,disableTocAnimation:c,render:n,updateToc:l}}},/*!*********************************!*\
   !*** ./src/js/parse-content.js ***!
   \*********************************/
 function(e,t){e.exports=function(e){function t(e){return e[e.length-1]}function n(e){return+e.nodeName.split("H").join("")}function o(t){var o={id:t.id,children:[],nodeName:t.nodeName,headingLevel:n(t),textContent:t.textContent.trim()};return e.includeHtml&&(o.childNodes=t.childNodes),o}function i(i,r){for(var l=o(i),s=n(i),c=r,a=t(c),u=a?a.headingLevel:0,d=s-u;d>0;)a=t(c),a&&void 0!==a.children&&(c=a.children),d--;return s>=e.collapseDepth&&(l.isCollapsed=!0),c.push(l),c}function r(t,n){var o=n;e.ignoreSelector&&(o=n.split(",").map(function(t){return t.trim()+":not("+e.ignoreSelector+")"}));try{return document.querySelector(t).querySelectorAll(o)}catch(i){return console.warn("Element not found: "+t),null}}function l(e){return s.call(e,function(e,t){var n=o(t);return i(n,e.nest),e},{nest:[]})}var s=[].reduce;return{nestHeadingsArray:l,selectHeadings:r}}}]);
 //# sourceMappingURL=tocbot.min.js.map

</script>

<script type="text/javascript">

  var generateToc = function() {
    var readyState = document.readyState;
    if (readyState === 'interactive' || readyState == 'complete') {

      var headerSize = 140;

      var tocParentNode = document.createElement("div");
      tocParentNode.setAttribute("id", "toc-parent");

      var generatedTocNode = document.createElement("div");
      generatedTocNode.setAttribute("id", "generated-toc");
      tocParentNode.appendChild(generatedTocNode);

      var contentNode = document.getElementById("content");
      contentNode.parentNode.insertBefore(tocParentNode, contentNode);

      var clearNode = document.createElement("div");
      clearNode.setAttribute("class", "clear");
      contentNode.parentNode.appendChild(clearNode);

      tocbot.init({
        tocSelector: '#generated-toc',
        contentSelector: '#content',
        headingSelector: 'h2, h3, h4, h5',
        headingsOffset: headerSize,
        smoothScrollOptions: {
          easing: 'easeInOutCubic',
          offset: headerSize,
          speed: 300, // animation duration.
          updateURL: true
        },
        collapseDepth: 0
      });

    } else {
      window.setTimeout(function(){
        generateToc();
      }, 50);
    }

  }
  generateToc();

</script>
</head>
<body class="book">
<div id="header">
<h1>Flowable CMMN 用户手册 (v 6.4.2-SNAPSHOT)</h1>
</div>
<div id="content">
<div class="sect1">
<h2 id="_说明">1. 说明</h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="license">1.1. 许可证</h3>
<div class="paragraph">
<p>Flowable在链接下发布:http://www.apache.org/licenses/LICENSE-2.0.html[Apache V2许可证]</p>
</div>
</div>
<div class="sect2">
<h3 id="download">1.2. 下载</h3>
<div class="paragraph">
<p>下载url：http://www.flowable.org/downloads.html</p>
</div>
</div>
<div class="sect2">
<h3 id="sources">1.3. 来源</h3>
<div class="paragraph">
<p>(发行版包含了大部分作为JAR文件的源文件。Flowable的源代码可以在以下链接中找到url：https://github.com/flowable/flowable-engine)</p>
</div>
</div>
<div class="sect2">
<h3 id="experimental">1.4. 实验特性</h3>
<div class="paragraph">
<p>标有*[EXPERIMENTAL]*的章节介绍不应认为是稳定的
所有具有.impl的类，在包名中是内部实现类，不能以任何方式被认为是稳定的或有保证的。但是，如果用户指南将任何类提到为配置值，则支持它们，并且可以认为它们是稳定的。</p>
</div>
</div>
<div class="sect2">
<h3 id="required.software">1.5. 安装必需的软件</h3>
<div class="paragraph">
<p>运行Flowable需要JDK 8或以上版本。可以访问 Oracle Java SE downloads页面url：http://www.oracle.com/technetwork/java/javase/downloads/index.html。该页面上也有安装指导。安装完成后，可以执行 java -version 。能看到JDK的版本信息就说明安装成功了。</p>
</div>
</div>
<div class="sect2">
<h3 id="reporting.problems">1.6. 报告问题</h3>
<div class="paragraph">
<p>问题和评论可以在链接上讨论url:https://forum.flowable.org。问题可以在链接中创建url：https://github.com/flowable/flowengine/issue [我们的Github问题跟踪器]。</p>
</div>
</div>
<div class="sect2">
<h3 id="internal">1.7. 内部实现类</h3>
<div class="paragraph">
<p>在JAR文件中，包中的所有类都有.impl.在它们里面是实现类，应该被认为是内部的。对于实现类中的类或接口，没有提供稳定性保证。</p>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_配置">2. 配置</h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="configuration">2.1. 创建CmmnEngine</h3>
<div class="paragraph">
<p>Flowable CMMN引擎通过名为+flowable.cmmn.cfg.xml+的XML文件进行配置。 请注意，如果您使用的是“springintegration，构建流程引擎的Spring样式”，则这不适用*。</p>
</div>
<div class="paragraph">
<p>获得CmmnEngine的最简单方法是使用org.flowable.cmmn.engine.CmmnEngineConfiguration类：</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="java"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1</div></td><td class="code"><span></span><span class="tok-n">CmmnEngine</span> <span class="tok-n">cmmnEngine</span> <span class="tok-o">=</span> <span class="tok-n">CmmnEngineConfiguration</span><span class="tok-o">.</span><span class="tok-na">createStandaloneCmmnEngineConfiguration</span><span class="tok-o">();</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>这将在类路径上查找flowable.cmmn.cfg.xml文件，并根据该文件中的配置构造引擎。 以下代码段显示了示例配置。 以下部分将详细介绍配置属性。</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="xml"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv"> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15</div></td><td class="code"><span></span><span class="tok-nt">&lt;beans</span> <span class="tok-na">xmlns=</span><span class="tok-s">&quot;http://www.springframework.org/schema/beans&quot;</span>
       <span class="tok-na">xmlns:xsi=</span><span class="tok-s">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span>
       <span class="tok-na">xsi:schemaLocation=</span><span class="tok-s">&quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd&quot;</span><span class="tok-nt">&gt;</span>

  <span class="tok-nt">&lt;bean</span> <span class="tok-na">id=</span><span class="tok-s">&quot;cmmnEngineConfiguration&quot;</span> <span class="tok-na">class=</span><span class="tok-s">&quot;org.flowable.cmmn.engine.CmmnEngineConfiguration&quot;</span><span class="tok-nt">&gt;</span>

    <span class="tok-nt">&lt;property</span> <span class="tok-na">name=</span><span class="tok-s">&quot;jdbcUrl&quot;</span> <span class="tok-na">value=</span><span class="tok-s">&quot;jdbc:h2:mem:flowable;DB_CLOSE_DELAY=1000&quot;</span> <span class="tok-nt">/&gt;</span>
    <span class="tok-nt">&lt;property</span> <span class="tok-na">name=</span><span class="tok-s">&quot;jdbcDriver&quot;</span> <span class="tok-na">value=</span><span class="tok-s">&quot;org.h2.Driver&quot;</span> <span class="tok-nt">/&gt;</span>
    <span class="tok-nt">&lt;property</span> <span class="tok-na">name=</span><span class="tok-s">&quot;jdbcUsername&quot;</span> <span class="tok-na">value=</span><span class="tok-s">&quot;sa&quot;</span> <span class="tok-nt">/&gt;</span>
    <span class="tok-nt">&lt;property</span> <span class="tok-na">name=</span><span class="tok-s">&quot;jdbcPassword&quot;</span> <span class="tok-na">value=</span><span class="tok-s">&quot;&quot;</span> <span class="tok-nt">/&gt;</span>

    <span class="tok-nt">&lt;property</span> <span class="tok-na">name=</span><span class="tok-s">&quot;databaseSchemaUpdate&quot;</span> <span class="tok-na">value=</span><span class="tok-s">&quot;true&quot;</span> <span class="tok-nt">/&gt;</span>
  <span class="tok-nt">&lt;/bean&gt;</span>

<span class="tok-nt">&lt;/beans&gt;</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>请注意，配置XML实际上是一个Spring配置。 这并不意味着Flowable只能在Spring环境中使用！我们只是在内部利用Spring的解析和依赖注入功能来构建引擎。</p>
</div>
<div class="paragraph">
<p>也可以使用配置文件以编程方式创建CmmnEngineConfiguration对象。 也可以使用不同的bean id（例如，参见第3行）。</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="java"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1
2
3
4
5
6</div></td><td class="code"><span></span><span class="tok-n">CmmnEngineConfiguration</span><span class="tok-o">.</span>
  <span class="tok-nf">createCmmnEngineConfigurationFromResourceDefault</span><span class="tok-o">();</span>
  <span class="tok-n">createCmmnEngineConfigurationFromResource</span><span class="tok-o">(</span><span class="tok-n">String</span> <span class="tok-n">resource</span><span class="tok-o">);</span>
  <span class="tok-n">createCmmnEngineConfigurationFromResource</span><span class="tok-o">(</span><span class="tok-n">String</span> <span class="tok-n">resource</span><span class="tok-o">,</span> <span class="tok-n">String</span> <span class="tok-n">beanName</span><span class="tok-o">);</span>
  <span class="tok-n">createCmmnEngineConfigurationFromInputStream</span><span class="tok-o">(</span><span class="tok-n">InputStream</span> <span class="tok-n">inputStream</span><span class="tok-o">);</span>
  <span class="tok-n">createCmmnEngineConfigurationFromInputStream</span><span class="tok-o">(</span><span class="tok-n">InputStream</span> <span class="tok-n">inputStream</span><span class="tok-o">,</span> <span class="tok-n">String</span> <span class="tok-n">beanName</span><span class="tok-o">);</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>也可以不使用配置文件，并基于创建配置默认值（有关更多信息，请参阅&lt;&lt; configurationClasses，不同支持的类&gt;&gt;）。</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="java"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1
2</div></td><td class="code"><span></span><span class="tok-n">CmmnEngineConfiguration</span><span class="tok-o">.</span><span class="tok-na">createStandaloneCmmnEngineConfiguration</span><span class="tok-o">();</span>
<span class="tok-n">CmmnEngineConfiguration</span><span class="tok-o">.</span><span class="tok-na">createStandaloneInMemCmmnEngineConfiguration</span><span class="tok-o">();</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>所有这些CmmnEngineConfiguration.createXXX（）方法返回CmmnEngineConfiguration，如果需要可以进一步调整。 调用buildCmmnEngine（）操作后，创建一个 CmmnEngine：</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="java"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1
2
3
4</div></td><td class="code"><span></span><span class="tok-n">CmmnEngine</span> <span class="tok-n">cmmnEngine</span> <span class="tok-o">=</span> <span class="tok-n">CmmnEngineConfiguration</span><span class="tok-o">.</span><span class="tok-na">createStandaloneInMemCmmnEngineConfiguration</span><span class="tok-o">()</span>
  <span class="tok-o">.</span><span class="tok-na">setDatabaseSchemaUpdate</span><span class="tok-o">(</span><span class="tok-n">CmmnEngineConfiguration</span><span class="tok-o">.</span><span class="tok-na">DB_SCHEMA_UPDATE_TRUE</span><span class="tok-o">)</span>
  <span class="tok-o">.</span><span class="tok-na">setJdbcUrl</span><span class="tok-o">(</span><span class="tok-s">&quot;jdbc:h2:mem:my-own-db;DB_CLOSE_DELAY=1000&quot;</span><span class="tok-o">)</span>
  <span class="tok-o">.</span><span class="tok-na">buildCmmnEngine</span><span class="tok-o">();</span>
</td></tr></table></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="configurationRoot">2.2. CmmnEngineConfiguration bean</h3>
<div class="paragraph">
<p>flowable.cmmn.cfg.xml必须包含一个具有id值为&#8217;cmmnEngineConfiguration&#8217;的bean。</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="xml"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1</div></td><td class="code"><span></span> <span class="tok-nt">&lt;bean</span> <span class="tok-na">id=</span><span class="tok-s">&quot;cmmnEngineConfiguration&quot;</span> <span class="tok-na">class=</span><span class="tok-s">&quot;org.flowable.cmmn.engine.CmmnEngineConfiguration&quot;</span><span class="tok-nt">&gt;</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>然后使用该bean构造CmmnEngine 。</p>
</div>
<div class="ulist">
<ul>
<li>
<p><strong>org.flowable.cmmn.engine.impl.cfg.StandaloneInMemCmmnEngineConfiguration</strong>: this is a convenience class for unit testing purposes. Flowable will take care of all transactions. An H2 in-memory database is used by default. The database will be created and dropped when the engine boots and shuts down. When using this, no additional configuration is probably needed.</p>
</li>
<li>
<p><strong>org.flowable.cmmn.spring.SpringCmmnEngineConfiguration</strong>: To be used when the CMMN engine is used in a Spring environment.  See <a href="#springintegration">the Spring integration section</a> for more information.</p>
</li>
</ul>
</div>
</div>
<div class="sect2">
<h3 id="databaseConfiguration">2.3. 数据源配置</h3>
<div class="paragraph">
<p>有两种方法可以配置Flowable CMMN引擎将使用的数据库。 第一个选项是定义数据库的JDBC属性：
* <strong>jdbcUrl</strong>: 数据库的JDBC URL.
* <strong>jdbcDriver</strong>: 为特定数据库类型实现驱动程序.
* <strong>jdbcUsername</strong>: 用于连接数据库的用户名.
* <strong>jdbcPassword</strong>: 用于连接数据库的密码.</p>
</div>
<div class="paragraph">
<p>基于提供的JDBC属性构造的数据源将具有默认链接： http：//www.mybatis.org/ [MyBatis]连接池设置。 可以选择设置以下属性来调整该连接池（取自MyBatis文档）：</p>
</div>
<div class="ulist">
<ul>
<li>
<p><strong>jdbcMaxActiveConnections</strong>: 连接池中处于被使用状态的连接的最大值。默认为10。</p>
</li>
<li>
<p><strong>jdbcMaxIdleConnections</strong>: 连接池中处于空闲状态的连接的最大值。</p>
</li>
<li>
<p><strong>jdbcMaxCheckoutTime</strong>: 连接被取出使用的最长时间，超过时间会被强制回收。 默认为20000（20秒）。</p>
</li>
<li>
<p><strong>jdbcMaxWaitTime</strong>:这是一个底层配置，让连接池可以在长时间无法获得连接时， 打印一条日志，并重新尝试获取一个连接。（避免因为错误配置导致沉默的操作失败）。 默认为20000（20秒）。
示例数据库配置：</p>
</li>
</ul>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="xml"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1
2
3
4</div></td><td class="code"><span></span><span class="tok-nt">&lt;property</span> <span class="tok-na">name=</span><span class="tok-s">&quot;jdbcUrl&quot;</span> <span class="tok-na">value=</span><span class="tok-s">&quot;jdbc:h2:mem:flowable;DB_CLOSE_DELAY=1000&quot;</span> <span class="tok-nt">/&gt;</span>
<span class="tok-nt">&lt;property</span> <span class="tok-na">name=</span><span class="tok-s">&quot;jdbcDriver&quot;</span> <span class="tok-na">value=</span><span class="tok-s">&quot;org.h2.Driver&quot;</span> <span class="tok-nt">/&gt;</span>
<span class="tok-nt">&lt;property</span> <span class="tok-na">name=</span><span class="tok-s">&quot;jdbcUsername&quot;</span> <span class="tok-na">value=</span><span class="tok-s">&quot;sa&quot;</span> <span class="tok-nt">/&gt;</span>
<span class="tok-nt">&lt;property</span> <span class="tok-na">name=</span><span class="tok-s">&quot;jdbcPassword&quot;</span> <span class="tok-na">value=</span><span class="tok-s">&quot;&quot;</span> <span class="tok-nt">/&gt;</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>我们的基准测试表明，在处理大量并发请求时，MyBatis连接池可能扛不住。 因此，我们建议使用javax.sql.DataSource实现并将其注入流程引擎配置（例如HikariCP，Tomcat JDBC连接池等）：</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="xml"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv"> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12</div></td><td class="code"><span></span><span class="tok-nt">&lt;bean</span> <span class="tok-na">id=</span><span class="tok-s">&quot;dataSource&quot;</span> <span class="tok-na">class=</span><span class="tok-s">&quot;org.apache.commons.dbcp.BasicDataSource&quot;</span> <span class="tok-nt">&gt;</span>
  <span class="tok-nt">&lt;property</span> <span class="tok-na">name=</span><span class="tok-s">&quot;driverClassName&quot;</span> <span class="tok-na">value=</span><span class="tok-s">&quot;com.mysql.jdbc.Driver&quot;</span> <span class="tok-nt">/&gt;</span>
  <span class="tok-nt">&lt;property</span> <span class="tok-na">name=</span><span class="tok-s">&quot;url&quot;</span> <span class="tok-na">value=</span><span class="tok-s">&quot;jdbc:mysql://localhost:3306/flowable&quot;</span> <span class="tok-nt">/&gt;</span>
  <span class="tok-nt">&lt;property</span> <span class="tok-na">name=</span><span class="tok-s">&quot;username&quot;</span> <span class="tok-na">value=</span><span class="tok-s">&quot;flowable&quot;</span> <span class="tok-nt">/&gt;</span>
  <span class="tok-nt">&lt;property</span> <span class="tok-na">name=</span><span class="tok-s">&quot;password&quot;</span> <span class="tok-na">value=</span><span class="tok-s">&quot;flowable&quot;</span> <span class="tok-nt">/&gt;</span>
  <span class="tok-nt">&lt;property</span> <span class="tok-na">name=</span><span class="tok-s">&quot;defaultAutoCommit&quot;</span> <span class="tok-na">value=</span><span class="tok-s">&quot;false&quot;</span> <span class="tok-nt">/&gt;</span>
<span class="tok-nt">&lt;/bean&gt;</span>

<span class="tok-nt">&lt;bean</span> <span class="tok-na">id=</span><span class="tok-s">&quot;cmmnEngineConfiguration&quot;</span> <span class="tok-na">class=</span><span class="tok-s">&quot;org.flowable.cmmn.engine.CmmnEngineConfiguration&quot;</span><span class="tok-nt">&gt;</span>

  <span class="tok-nt">&lt;property</span> <span class="tok-na">name=</span><span class="tok-s">&quot;dataSource&quot;</span> <span class="tok-na">ref=</span><span class="tok-s">&quot;dataSource&quot;</span> <span class="tok-nt">/&gt;</span>
  ...
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>请注意，Flowable 表单不附带允许您定义此类数据源的库。 所以你必须确保库在你的类路径上。</p>
</div>
<div class="paragraph">
<p>无论您使用的是JDBC还是数据源方法，都可以设置以下属性：</p>
</div>
<div class="ulist">
<ul>
<li>
<p><strong>databaseType</strong>: 数据库类型，可以是如下的值（h2, mysql, oracle, postgres, mssql, db2）.</p>
</li>
<li>
<p><strong>databaseSchemaUpdate</strong>:允许您设置策略以在表单引擎启动和关闭时如何处理数据库表.</p>
<div class="ulist">
<ul>
<li>
<p><code>false</code> (default): 在创建表单引擎时检查库模式的版本，如果版本不匹配则抛出异常.</p>
</li>
<li>
<p><code>true</code>: 在构建表单引擎时，执行检查并在必要时执行模式的更新。 如果schema不存在，则创建它.</p>
</li>
<li>
<p><code>create-drop</code>: 在创建表单引擎时创建schema，并在关闭流程引擎时删除schema.</p>
</li>
</ul>
</div>
</li>
</ul>
</div>
</div>
<div class="sect2">
<h3 id="jndiDatasourceConfig">2.4. JNDI方式数据源配置</h3>
<div class="paragraph">
<p>默认情况下，Flowable Form的数据库配置包含在每个Web应用程序的WEB-INF/classes中的db.properties文件中。 这并不总是理想的，因为它
要求用户修改Flowable源中的db.properties并重新编译WAR文件，或者在每次部署时分解WAR并修改db.properties。
通过使用JNDI（Java命名和目录接口）获取数据库连接，连接完全由Servlet容器管理，并且可以在WAR部署之外管理配置。 这也允许对db.properties文件提供的连接参数进行更多控制。</p>
</div>
<div class="sect3">
<h4 id="jndi_configuration">2.4.1. 配置</h4>
<div class="paragraph">
<p>JNDI数据源的配置将根据您使用的servlet容器应用程序而有所不同。 以下说明适用于Tomcat，但对于其他容器应用程序，请参阅容器应用程序的文档。</p>
</div>
<div class="paragraph">
<p>如果使用Tomcat，则在$CATALINA_BASE/conf/[enginename]/[hostname]/[warname].xml中配置JNDI资源（对于Flowable UI，这通常是$CATALINA_BASE/conf/Catalina/localhost/flowable-app。XML）。 首次部署应用程序时，将从Flowable WAR文件复制默认上下文，因此如果已存在，则需要替换它。 例如，要更改JNDI资源以便应用程序连接到MySQL而不是H2，请将文件更改为以下内容：</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="xml"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv"> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16</div></td><td class="code"><span></span><span class="tok-cp">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span>
    <span class="tok-nt">&lt;Context</span> <span class="tok-na">antiJARLocking=</span><span class="tok-s">&quot;true&quot;</span> <span class="tok-na">path=</span><span class="tok-s">&quot;/flowable-app&quot;</span><span class="tok-nt">&gt;</span>
        <span class="tok-nt">&lt;Resource</span> <span class="tok-na">auth=</span><span class="tok-s">&quot;Container&quot;</span>
            <span class="tok-na">name=</span><span class="tok-s">&quot;jdbc/flowableDB&quot;</span>
            <span class="tok-na">type=</span><span class="tok-s">&quot;javax.sql.DataSource&quot;</span>
            <span class="tok-na">description=</span><span class="tok-s">&quot;JDBC DataSource&quot;</span>
            <span class="tok-na">url=</span><span class="tok-s">&quot;jdbc:mysql://localhost:3306/flowable&quot;</span>
            <span class="tok-na">driverClassName=</span><span class="tok-s">&quot;com.mysql.jdbc.Driver&quot;</span>
            <span class="tok-na">username=</span><span class="tok-s">&quot;sa&quot;</span>
            <span class="tok-na">password=</span><span class="tok-s">&quot;&quot;</span>
            <span class="tok-na">defaultAutoCommit=</span><span class="tok-s">&quot;false&quot;</span>
            <span class="tok-na">initialSize=</span><span class="tok-s">&quot;5&quot;</span>
            <span class="tok-na">maxWait=</span><span class="tok-s">&quot;5000&quot;</span>
            <span class="tok-na">maxActive=</span><span class="tok-s">&quot;120&quot;</span>
            <span class="tok-na">maxIdle=</span><span class="tok-s">&quot;5&quot;</span><span class="tok-nt">/&gt;</span>
        <span class="tok-nt">&lt;/Context&gt;</span>
</td></tr></table></code></pre>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_jndi_属性">2.4.2. JNDI 属性</h4>
<div class="paragraph">
<p>要配置JNDI数据源，请在Flowable UI的属性文件中使用以下属性：</p>
</div>
<div class="ulist">
<ul>
<li>
<p>spring.datasource.jndi-name=: 数据源的JNDI名称.</p>
</li>
<li>
<p>datasource.jndi.resourceRef: 设置查询是否发生在J2EE容器中，换句话说，如果JNDI名称尚未包含它，则需要添加前缀“java：comp/env/”。 默认为“true”.</p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect2">
<h3 id="supporteddatabases">2.5. 支持的数据库厂商</h3>
<div class="paragraph">
<p>下面列出了Flowable用于引用数据库的类型（区分大小写！）。ses.</p>
</div>
<table id="databaseTypes" class="tableblock frame-all grid-all stretch">
<colgroup>
<col style="width: 33.3333%;">
<col style="width: 33.3333%;">
<col style="width: 33.3334%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">数据库类型</th>
<th class="tableblock halign-left valign-top">连接URL</th>
<th class="tableblock halign-left valign-top">Notes</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">h2</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">jdbc:h2:tcp://localhost/flowable_form</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Default configured database</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">mysql</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">jdbc:mysql://localhost:3306/flowable_form?autoReconnect=true</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Tested using mysql-connector-java database driver</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">oracle</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">jdbc:oracle:thin:@localhost:1521:xe</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">postgres</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">jdbc:postgresql://localhost:5432/flowable_form</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">db2</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">jdbc:db2://localhost:50000/flowable_form</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">mssql</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">jdbc:sqlserver://localhost:1433;databaseName=flowable_form (jdbc.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver) <em>OR</em> jdbc:jtds:sqlserver://localhost:1433/flowable_form (jdbc.driver=net.sourceforge.jtds.jdbc.Driver)</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Tested using Microsoft JDBC Driver 4.0 (sqljdbc4.jar) and JTDS Driver</p></td>
</tr>
</tbody>
</table>
</div>
<div class="sect2">
<h3 id="creatingDatabaseTable">2.6. 创建表</h3>
<div class="paragraph">
<p>为数据库创建数据库表的最简单方法是：</p>
</div>
<div class="ulist">
<ul>
<li>
<p>在classpath中添加flowable-cmmn-engine JARS包</p>
</li>
<li>
<p>添加合适的数据库驱动</p>
</li>
<li>
<p>将Flowable配置文件（flowable.cmmn.cfg.xml）添加到类路径中，指向您的数据库（请参阅&lt;&lt; databaseConfiguration，数据库配置部分&gt;&gt;）</p>
</li>
<li>
<p>执行DbSchemaCreate类的main方法</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>但是，通常只有数据库管理员才能在数据库上执行DDL语句。 在生产系统中，这也是最明智的选择。 可以在Flowable下载页面或Flowable分发文件夹内的database 子目录中找到SQL DDL语句。 这些脚本也在引擎JAR（flowable-cmmn-engine-x.jar）中，在包org/ flowable/cmmn/db /create中。 SQL文件的形式</p>
</div>
<div class="listingblock">
<div class="content">
<pre>flowable.{db}.cmmn.create.sql</pre>
</div>
</div>
<div class="paragraph">
<p>其中<em>db</em>是&lt;&lt; supporteddatabases，supported databases &gt;&gt;中的任何一个。</p>
</div>
</div>
<div class="sect2">
<h3 id="database.tables.explained">2.7. 数据库表名称解释</h3>
<div class="paragraph">
<p>Flowable CMMN Engine的数据库名称均以* ACT_CMMN_ *开头。</p>
</div>
<div class="ulist">
<ul>
<li>
<p><strong>ACT_CMMN_</strong>*: 没有附加前缀的表包含“static”信息，例如案例定义和案例资源（图像，规则等）。</p>
</li>
<li>
<p><strong>ACT_CMMN_RU_</strong>*: 'RU&#8217;代表+运行时+。 这些是包含案例实例，计划项目等的运行时数据的运行时表。 Flowable仅在案例实例执行期间存储运行时数据，并在案例实例结束时删除记录。 这使运行时表保持小而快。</p>
</li>
<li>
<p><strong>ACT_CMMN_HI_</strong>*:'HI&#8217;代表历史。 这些是包含历史数据的表，例如过去的案例实例，计划项等。</p>
</li>
</ul>
</div>
</div>
<div class="sect2">
<h3 id="databaseUpgrade">2.8. 数据库升级</h3>
<div class="paragraph">
<p>在运行升级之前，请确保备份数据库（使用数据库备份功能）。</p>
</div>
<div class="paragraph">
<p>默认情况下，每次创建流程引擎时都会执行版本检查。 这通常在应用程序或Flowable Web应用程序的引导时发生一次。 如果Flowable库注意到库版本与Flowable数据库表的版本之间的差异，则抛出异常。</p>
</div>
<div class="paragraph">
<p>要升级，必须首先将以下配置属性放在flowable.cmmn.cfg.xml配置文件中：</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="xml"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv"> 1
 2
 3
 4
 5
 6
 7
 8
 9
10</div></td><td class="code"><span></span><span class="tok-nt">&lt;beans</span> <span class="tok-nt">&gt;</span>

  <span class="tok-nt">&lt;bean</span> <span class="tok-na">id=</span><span class="tok-s">&quot;cmmnEngineConfiguration&quot;</span>
      <span class="tok-na">class=</span><span class="tok-s">&quot;org.flowable.cmmn.engine.CmmnEngineConfiguration&quot;</span><span class="tok-nt">&gt;</span>
    <span class="tok-c">&lt;!-- ... --&gt;</span>
    <span class="tok-nt">&lt;property</span> <span class="tok-na">name=</span><span class="tok-s">&quot;databaseSchemaUpdate&quot;</span> <span class="tok-na">value=</span><span class="tok-s">&quot;true&quot;</span> <span class="tok-nt">/&gt;</span>
    <span class="tok-c">&lt;!-- ... --&gt;</span>
  <span class="tok-nt">&lt;/bean&gt;</span>

<span class="tok-nt">&lt;/beans&gt;</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="literalblock">
<div class="content">
<pre>使用databaseSchemaUpdate设置为true即可完成自动升级。</pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="historyConfiguration">2.9. 历史配置</h3>
<div class="paragraph">
<p>自定义历史存储的配置是可选的。 这允许您调整影响引擎的“历史记录，历史记录功能”的设置。 有关详细信息，请参阅&lt;&lt; historyConfig，history configuration &gt;&gt;。</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="xml"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1</div></td><td class="code"><span></span><span class="tok-nt">&lt;property</span> <span class="tok-na">name=</span><span class="tok-s">&quot;history&quot;</span> <span class="tok-na">value=</span><span class="tok-s">&quot;audit&quot;</span> <span class="tok-nt">/&gt;</span>
</td></tr></table></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="exposingConfigurationBeans">2.10. 在表达式和脚本中公开配置bean</h3>
<div class="paragraph">
<p>默认情况下，您在flowable.cmmn.cfg.xml配置或您自己的Spring配置文件中指定的所有bean都可用于表达式和脚本。 如果要限制配置文件中bean的可见性，可以在流程引擎配置中配置名为beans的属性。 CmmnEngineConfiguration中的beans属性是一个map。 指定该属性时，表达式和脚本只能看到该映射中指定的bean。 暴露的bean将使用您在地图中指定的名称公开。</p>
</div>
</div>
<div class="sect2">
<h3 id="caseDefinitionCacheConfiguration">2.11. 部署缓存配置</h3>
<div class="paragraph">
<p>所有定义都被缓存（在解析之后），以避免每次需要表单时都访问数据库，并且表单数据不会更改。 默认情况下，此缓存没有限制。 要限制表单缓存使用的容器大小，请添加以下属性：</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="xml"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1</div></td><td class="code"><span></span><span class="tok-nt">&lt;property</span> <span class="tok-na">name=</span><span class="tok-s">&quot;caseDefinitionCacheLimit&quot;</span> <span class="tok-na">value=</span><span class="tok-s">&quot;10&quot;</span> <span class="tok-nt">/&gt;</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>设置此属性将使默认的LRU算法。 当然，此属性的“最佳”值取决于存储的案例总量和运行时实际使用的案例数。</p>
</div>
<div class="paragraph">
<p>您也可以注入自己的缓存实现。 这必须是实现org.flowable.engine.common.impl.persistence.deploy.DeploymentCache接口的bean：</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="xml"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1
2
3</div></td><td class="code"><span></span><span class="tok-nt">&lt;property</span> <span class="tok-na">name=</span><span class="tok-s">&quot;caseDefinitionCache&quot;</span><span class="tok-nt">&gt;</span>
  <span class="tok-nt">&lt;bean</span> <span class="tok-na">class=</span><span class="tok-s">&quot;org.flowable.MyCache&quot;</span> <span class="tok-nt">/&gt;</span>
<span class="tok-nt">&lt;/property&gt;</span>
</td></tr></table></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="loggingConfiguration">2.12. 日志</h3>
<div class="paragraph">
<p>所有日志记录（flowable，spring，mybatis，&#8230;&#8203;）都通过SLF4J进行路由，并允许选择您选择的日志记录实现。</p>
</div>
<div class="paragraph">
<p>*默认情况下，flowable-dmn-engine依赖项中不存在SFL4J-binding jar，这应该在项目中添加，以便使用您选择的日志框架。
*如果没有添加实现jar，SLF4J将使用NOP-logger，不记录任何内容，除了警告不会记录任何内容。 有关这些绑定链接的更多信息，请访问：http://www.slf4j.org/codes.html#StaticLoggerBinder[<a href="http://www.slf4j.org/codes.html#StaticLoggerBinder" class="bare">http://www.slf4j.org/codes.html#StaticLoggerBinder</a>]。
使用Maven，添加例如这样的依赖（这里使用log4j），请注意您仍然需要添加一个版本：</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="xml"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1
2
3
4</div></td><td class="code"><span></span><span class="tok-nt">&lt;dependency&gt;</span>
  <span class="tok-nt">&lt;groupId&gt;</span>org.slf4j<span class="tok-nt">&lt;/groupId&gt;</span>
  <span class="tok-nt">&lt;artifactId&gt;</span>slf4j-log4j12<span class="tok-nt">&lt;/artifactId&gt;</span>
<span class="tok-nt">&lt;/dependency&gt;</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>flowable-ui和flowable-rest webapps配置了使用Log4j binding.。 在运行所有flowable-*模块的测试时也使用Log4j。</p>
</div>
<div class="paragraph">
<p>在类路径中使用带有commons-logging的容器时的重要注意事项：
为了通过SLF4J路由spring-logging，使用了一个桥接器（参见链接：http://www.slf4j.org/legacy.html#jclOverSLF4J[<a href="http://www.slf4j.org/legacy.html#jclOverSLF4J" class="bare">http://www.slf4j.org/legacy.html#jclOverSLF4J</a>]）。
如果您的容器提供了commons-logging实现，请按照此页面上的说明进行操作：http://www.slf4j.org/codes.html#release[<a href="http://www.slf4j.org/codes.html#release" class="bare">http://www.slf4j.org/codes.html#release</a>]确保稳定性。</p>
</div>
<div class="paragraph">
<p>使用Maven时的示例（省略版本）：</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="xml"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1
2
3
4</div></td><td class="code"><span></span><span class="tok-nt">&lt;dependency&gt;</span>
  <span class="tok-nt">&lt;groupId&gt;</span>org.slf4j<span class="tok-nt">&lt;/groupId&gt;</span>
  <span class="tok-nt">&lt;artifactId&gt;</span>jcl-over-slf4j<span class="tok-nt">&lt;/artifactId&gt;</span>
<span class="tok-nt">&lt;/dependency&gt;</span>
</td></tr></table></code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="chapterApi">3. The Flowable CMMN API</h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="apiEngine">3.1. 流程 CMMN 引擎 API 与服务类</h3>
<div class="paragraph">
<p>引擎 API 是与 Flowable 交互的最常用方式。核心起点是可以通过<a href="#configuration">配置部分</a>中描述的几种方式创建的 <code>CMMN 引擎</code>。从 CMMN 引擎中，您可以获得包含案例/CMMN 方法的各种服务类。CMMN 引擎和其服务类对象是线程安全的，因此您可以为整个服务保留其中一个引用。</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="java"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1
2
3
4
5
6
7</div></td><td class="code"><span></span><span class="tok-n">CmmnEngine</span> <span class="tok-n">cmmnEngine</span> <span class="tok-o">=</span> <span class="tok-n">CmmnEngineConfiguration</span><span class="tok-o">.</span><span class="tok-na">createStandaloneCmmnEngineConfiguration</span><span class="tok-o">();</span>

<span class="tok-n">CmmnRuntimeService</span> <span class="tok-n">runtimeService</span> <span class="tok-o">=</span> <span class="tok-n">cmmnEngine</span><span class="tok-o">.</span><span class="tok-na">getCmmnRuntimeService</span><span class="tok-o">();</span>
<span class="tok-n">CmmnRepositoryService</span> <span class="tok-n">repositoryService</span> <span class="tok-o">=</span> <span class="tok-n">cmmnEngine</span><span class="tok-o">.</span><span class="tok-na">getCmmnRepositoryService</span><span class="tok-o">();</span>
<span class="tok-n">CmmnTaskService</span> <span class="tok-n">taskService</span> <span class="tok-o">=</span> <span class="tok-n">cmmnEngine</span><span class="tok-o">.</span><span class="tok-na">getCmmnTaskService</span><span class="tok-o">();</span>
<span class="tok-n">CmmnManagementService</span> <span class="tok-n">managementService</span> <span class="tok-o">=</span> <span class="tok-n">cmmnEngine</span><span class="tok-o">.</span><span class="tok-na">getCmmnManagementService</span><span class="tok-o">();</span>
<span class="tok-n">CmmnHistoryService</span> <span class="tok-n">historyService</span> <span class="tok-o">=</span> <span class="tok-n">cmmnEngine</span><span class="tok-o">.</span><span class="tok-na">getCmmnHistoryService</span><span class="tok-o">();</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p><code>CmmnEngineConfiguration.createStandaloneCmmnEngineConfiguration()</code> 将初始化并构建 CMMN 引擎，然后始终返回这个 CMMN 引擎。</p>
</div>
<div class="paragraph">
<p>CmmnEngineConfiguration 类将扫描所有 <code>flowable.cmmn.cfg.xml</code> 和 <code>flowable-cmmn-context.xml</code> 配置文件。对于所有 <code>flowable.cmmn.cfg.xml</code> 配置文件，CMMN 引擎将以典型的 Flowable 方式构建：<code>CmmnEngineConfiguration.createCmmnEngineConfigurationFromInputStream(inputStream).buildCmmnEngine()</code>。对于所有 <code>flowable-cmmn-context.xml</code> 配置文件，CMMN 引擎将以 Spring 方式构建：首先创建 Spring 应用程序上下文，然后 CMMN 引擎会从该应用程序上下文中获取。</p>
</div>
<div class="paragraph">
<p>所有的服务类均是无状态的。这意味着您可以轻松地在群集中的多个节点上运行Flowable，每个节点都连接同一个数据库，而不必担心哪台机器实际执行了旧的调用。对任何服务类的任何调用都是幂等的，无论它在何处执行。</p>
</div>
<div class="paragraph">
<p><strong>CmmnRepositoryService</strong> 可能是 Flowable CMMN 引擎工作时所需的第一个服务类。该服务类提供用于管理和使用<code>部署文件</code>和<code>案例定义</code>的操作。案例定义是 CMMN 1.1案例的 Java 对应物，它表示案例的每个步骤的结构和行为，这里不再过多介绍。<code>部署文件</code>是 Flowable CMMN 引擎中的打包单元。部署文件可以包含多个CMMN 1.1 XML文件和任何其他资源，其中具体包含哪些内容取决于开发人员。它的范围可以从单个流程 CMMN 1.1 XML 文件到整个案例和相关资源的包（例如，部署文件“hr-cases”可能包含与 HR 案例相关的所有内容）。<code>CmmnRepositoryService</code> 可以<code>发布</code>这样的包。发布一个部署文件意味着将其上载到引擎，在该引擎中，所有案例在存储到数据库之前都会被检查和解析。从那一刻起，系统会感知到这个部署文件，并且部署文件中包含的任何流程都可以被发起。</p>
</div>
<div class="paragraph">
<p>此外，这个服务类允许您：</p>
</div>
<div class="ulist">
<ul>
<li>
<p>查询引擎已知的部署文件和案例定义。</p>
</li>
<li>
<p>检索各种资源，例如部署文件中包含的文件或引擎自动生成的案例图。</p>
</li>
<li>
<p>检索案例定义的 POJO 版本，它可用于使用 Java 而不是 JSON 进行内省。</p>
</li>
</ul>
</div>
<div class="paragraph">
<p><code>CmmnRepositoryService</code> 主要与静态信息（数据不会改变，至少不会变很多）有关，而 <strong>CmmnRuntimeService</strong> 恰恰相反，它涉及启动案例定义的新案例实例。如上所述，<code>案例定义</code>定义了案例的不同步骤的结构和行为。案例实例是案例定义的一次执行。对于每个案例定义，通常会有许多实例同时运行。CmmnRuntimeService 也是用于检索和存储案例变量的服务类。变量是特定于指定案例实例的数据，并且可以让案例中的各种结构使用（例如，方案的跳转条件通常使用流程变量来确定选择哪个路径来继续流转该案例）。CmmnRuntimeService 还允许您查询案例实例和方案条目。方案条目是 CMMN 1.1的已启用方案条目的表示。最后，无论何时案例实例需要继续流转并等待外部触发时都会使用 CmmnRuntimeService。案例实例可以具有各种等待状态，该服务类包含各种操作来向实例“发信号”并由外部触发器接收，然后案例实例可以继续流转。</p>
</div>
<div class="paragraph">
<p>需要由系统的操作用户执行的任务是 Flowable 这类 CMMN 引擎的核心。围绕任务的所有内容都分组在 <strong>CmmnTaskService</strong> 中，例如：</p>
</div>
<div class="ulist">
<ul>
<li>
<p>查询分配给用户或组的任务</p>
</li>
<li>
<p>创建新的独立任务，这些任务与流程实例无关。</p>
</li>
<li>
<p>操作派遣任务的参与人或以某种方式参与任务的用户。</p>
</li>
<li>
<p>签收并完成一个任务。 签收意味着某人决定成为该任务的操作人，这意味着该用户将审批该任务，审批意味着“完成任务”。通常这是填写一种表单。</p>
</li>
</ul>
</div>
<div class="paragraph">
<p><strong>CmmnHistoryService</strong> 暴露 Flowable CMMN 引擎收集的所有历史数据。在执行案例时，引擎可以保存大量数据（可以通过配置决定是否启用），例如案例实例开始时间，谁执行哪些任务，审批任务花费多长时间，每个案例实例流经哪条路径等。此服务类主要暴露查询功能以访问此类数据。</p>
</div>
<div class="paragraph">
<p><strong>CmmnManagementService</strong> 提供对数据库表低级信息的访问，允许查询不同类型的工作并执行它们。</p>
</div>
<div class="paragraph">
<p>有关服务操作和引擎 API 的更多详细信息，请参阅<a href="http://www.flowable.org/docs/javadocs/index.html">文档</a>。</p>
</div>
</div>
<div class="sect2">
<h3 id="_异常策略">3.2. 异常策略</h3>
<div class="paragraph">
<p>Flowable 中的基础异常是 <code>org.flowable.engine.FlowableException</code>，这是一个未经检查的异常。这种异常可以被 API 随时抛出，其他特定方法中发生的“预期中的”异常被记录在<a href="http://www.flowable.org/docs/javadocs/index.html">文档</a>中。例如 <code>CmmnTaskService</code> 的摘录：</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="java"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1
2
3
4
5
6</div></td><td class="code"><span></span><span class="tok-cm">/**</span>
<span class="tok-cm"> * 成功执行任务时调用。</span>
<span class="tok-cm"> * @param taskId 要完成的任务的id，不能为空。</span>
<span class="tok-cm"> * @throws FlowableObjectNotFoundException 当指定id的任务不存在时抛出。</span>
<span class="tok-cm"> */</span>
 <span class="tok-kt">void</span> <span class="tok-nf">complete</span><span class="tok-o">(</span><span class="tok-n">String</span> <span class="tok-n">taskId</span><span class="tok-o">);</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>在上面的示例中，当传递不存在的任务 id 时，将抛出异常。同样的，由于 Java 文档<strong>明确声明 taskId 不能为 null，因此在传递 <code>null</code> 时将抛出 <code>FlowableIllegalArgumentException</code></strong> 。</p>
</div>
<div class="paragraph">
<p>尽管我们想要避免一个大的异常层次结构，下列的异常子类仍会在特定情况下被抛出。在流程执行或 API 调用期间发生的所有其他错误都不适合下面可能发生的异常，这些错误将作为常规 <code>FlowableExceptions</code> 抛出。</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>FlowableWrongDbException</code>：当 Flowable 引擎发现数据库 Schema 版本与引擎版本不匹配时抛出。</p>
</li>
<li>
<p><code>FlowableOptimisticLockingException</code>：当由同一数据条目的并发访问引起的数据存储中发生乐观锁问题时抛出。</p>
</li>
<li>
<p><code>FlowableClassLoadingException</code>：当未找到请求加载的类或加载时发生错误时抛出（例如 Java 代理类、任务监听器等）。</p>
</li>
<li>
<p><code>FlowableObjectNotFoundException</code>：当请求或操作的对象不存在时抛出。</p>
</li>
<li>
<p><code>FlowableIllegalArgumentException</code>：这个异常表示在 Flowable API 调用中使用了非法参数、在引擎的配置中配置了非法值。</p>
</li>
<li>
<p><code>FlowableTaskAlreadyClaimedException</code>：当一个任务已经被签收时，再次调用 <code>taskService.claim(&#8230;&#8203;)</code> 时抛出。</p>
</li>
</ul>
</div>
</div>
<div class="sect2">
<h3 id="queryAPI">3.3. 查询 API</h3>
<div class="paragraph">
<p>有两种方法可以从引擎查询数据：使用查询 API 和本地查询。查询 API 允许您使用流畅的 API 编写完全类型安全的查询。您可以为查询添加各种查询条件（所有条件共同应用逻辑与）与一个排序参数。示例如下：</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="java"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1
2
3
4</div></td><td class="code"><span></span><span class="tok-n">List</span><span class="tok-o">&lt;</span><span class="tok-n">Task</span><span class="tok-o">&gt;</span> <span class="tok-n">tasks</span> <span class="tok-o">=</span> <span class="tok-n">taskService</span><span class="tok-o">.</span><span class="tok-na">createTaskQuery</span><span class="tok-o">()</span>
    <span class="tok-o">.</span><span class="tok-na">taskAssignee</span><span class="tok-o">(</span><span class="tok-s">&quot;kermit&quot;</span><span class="tok-o">)</span>
    <span class="tok-o">.</span><span class="tok-na">orderByDueDate</span><span class="tok-o">().</span><span class="tok-na">asc</span><span class="tok-o">()</span>
    <span class="tok-o">.</span><span class="tok-na">list</span><span class="tok-o">();</span>
</td></tr></table></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="apiVariables">3.4. 变量</h3>
<div class="paragraph">
<p>每个案例实例都需要并使用数据来执行自身节点来进行流转。在 Flowable 中, 这种数据被称为<em>变量</em>, 它们存储在数据库中。变量可以在表达式中使用（例如在哨兵的条件中）、在调用外部服务时的 Java 服务任务中使用（例如提供输入或存储服务调用的结果）等。</p>
</div>
<div class="paragraph">
<p>案例实例可以包含变量（称为<em>案例变量</em>）, 同样的<em>方案条目实例</em>和人工任务也可以包含变量。案例实例可以包含任意数量的变量。每个变量都存储在 <em>ACT_RU_VARIABLE</em> 数据库表的一行中。</p>
</div>
<div class="paragraph">
<p><em>createCaseInstanceBuilder_方法具有可选方法，用于通过 _CmmnRuntimeService</em> 创建案例实例并启动时提供变量：</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="java"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1</div></td><td class="code"><span></span><span class="tok-n">CaseInstance</span> <span class="tok-n">caseInstance</span> <span class="tok-o">=</span> <span class="tok-n">runtimeService</span><span class="tok-o">.</span><span class="tok-na">createCaseInstanceBuilder</span><span class="tok-o">().</span><span class="tok-na">variable</span><span class="tok-o">(</span><span class="tok-s">&quot;var1&quot;</span><span class="tok-o">,</span> <span class="tok-s">&quot;test&quot;</span><span class="tok-o">).</span><span class="tok-na">start</span><span class="tok-o">();</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>在案例执行期间可以添加变量。例如 <em>CmmnRuntimeService</em>：</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="java"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1</div></td><td class="code"><span></span><span class="tok-kt">void</span> <span class="tok-nf">setVariables</span><span class="tok-o">(</span><span class="tok-n">String</span> <span class="tok-n">caseInstanceId</span><span class="tok-o">,</span> <span class="tok-n">Map</span><span class="tok-o">&lt;</span><span class="tok-n">String</span><span class="tok-o">,</span> <span class="tok-o">?</span> <span class="tok-kd">extends</span> <span class="tok-n">Object</span><span class="tok-o">&gt;</span> <span class="tok-n">variables</span><span class="tok-o">);</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>如下所示，变量同样可以被检索。请注意 <em>CmmnTaskService</em> 上存在类似的方法。</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="java"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1
2</div></td><td class="code"><span></span><span class="tok-n">Map</span><span class="tok-o">&lt;</span><span class="tok-n">String</span><span class="tok-o">,</span> <span class="tok-n">Object</span><span class="tok-o">&gt;</span> <span class="tok-nf">getVariables</span><span class="tok-o">(</span><span class="tok-n">String</span> <span class="tok-n">caseInstanceId</span><span class="tok-o">);</span>
<span class="tok-n">Object</span> <span class="tok-nf">getVariable</span><span class="tok-o">(</span><span class="tok-n">String</span> <span class="tok-n">caseInstanceId</span><span class="tok-o">,</span> <span class="tok-n">String</span> <span class="tok-n">variableName</span><span class="tok-o">);</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>变量通常用于 Java 服务任务、达式和脚本中等。</p>
</div>
</div>
<div class="sect2">
<h3 id="apiTransientVariables">3.5. 临时变量</h3>
<div class="paragraph">
<p>临时变量是行为类似于常规变量的变量，但不是持久变量。通常，临时变量用于高级用例。如有疑问，请使用常规案例变量。</p>
</div>
<div class="paragraph">
<p>以下情况适用于临时变量：</p>
</div>
<div class="ulist">
<ul>
<li>
<p>临时变量根本不会存储历史记录。</p>
</li>
<li>
<p>与<em>常规</em>变量一样，临时变量在设置时会放在<em>最高级父节点</em>上。这意味着在方案条目上设置变量时，临时变量实际存储在案例执行实例中。与常规变量一样，如果在特定方案条目或任务上设置变量，则存在方法的<em>本地</em>变体。</p>
</li>
<li>
<p>临时变量只能在案例定义中的下一个“等待状态”之前访问。在那之后无法访问。在这里，等待状态表示案例实例中持久化到数据存储的时间点。</p>
</li>
<li>
<p>临时变量只能由 <em>setTransientVariable(name, value)</em> 设置，但在调用 <em>getVariable(name)</em> 时也会返回临时变量（临时变量在 <em>getTransientVariable(name)</em> 中也存在，它只检查瞬态变量）。这样做的原因是使表达式的编写变得容易，并且使用变量的现有逻辑适用于这两种变量类型。</p>
</li>
<li>
<p>临时变量会<em>优先于</em>相同名称的持久变量。这意味着当在同一案例实例上设置相同名称的持久变量和临时变量后调用 <em>getVariable("someVariable")</em> 时，将返回临时变量值。</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>您可以在暴露常规变量的大多数地方设置和获取临时变量：</p>
</div>
<div class="ulist">
<ul>
<li>
<p>在 <em>PlanItemJavaDelegate</em> 实现中的 <em>DelegatePlanItemInstance</em> 上</p>
</li>
<li>
<p>通过运行时服务类启动案例实例时</p>
</li>
<li>
<p>审批一个任务时</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>这些方法遵循常规案例变量的命名约定：</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="java"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1</div></td><td class="code"><span></span><span class="tok-n">CaseInstance</span> <span class="tok-n">caseInstance</span> <span class="tok-o">=</span> <span class="tok-n">runtimeService</span><span class="tok-o">.</span><span class="tok-na">createCaseInstanceBuilder</span><span class="tok-o">().</span><span class="tok-na">transientVariable</span><span class="tok-o">(</span><span class="tok-s">&quot;var1&quot;</span><span class="tok-o">,</span> <span class="tok-s">&quot;test&quot;</span><span class="tok-o">).</span><span class="tok-na">start</span><span class="tok-o">();</span>
</td></tr></table></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="apiExpressions">3.6. 表达式</h3>
<div class="paragraph">
<p>Flowable 使用 UEL 进行表达式解析。UEL 即 <em>Unified Expression Language（统一表达语言）</em>并且是 Java EE 6规范的一部分（详情参见 <a href="http://docs.oracle.com/javaee/6/tutorial/doc/gjddd.html">Java EE6 规范</a> ）。</p>
</div>
<div class="paragraph">
<p>表达式可用于例如 Java 服务任务和方案条目流转。虽然有两种类型的表达式：值表达式和方法表达式，但 Flowable 对此进行了抽象，因此它们都可以在需要<code>表达式</code>的地方使用。</p>
</div>
<div class="ulist">
<ul>
<li>
<p><strong>值表达式</strong>：解析为一个值。默认情况下，可以使用所有案例变量。此外，所有 Spring-beans（如果使用 Spring 的话）都可用于表达式。一些例子：</p>
</li>
</ul>
</div>
<div class="listingblock">
<div class="content">
<pre>${myVar}
${myBean.myProperty}</pre>
</div>
</div>
<div class="ulist">
<ul>
<li>
<p><strong>方法表达式</strong>：调用带或不带参数的方法。<strong>在调用不带参数的方法时，请确保在方法名称后添加空括号（因为这会将方法表达式与值表达式区分开来）。</strong>传递的参数可以是文本值或自己解析的表达式。例子：</p>
</li>
</ul>
</div>
<div class="listingblock">
<div class="content">
<pre>${printer.print()}
${myBean.addNewOrder('orderName')}
${myBean.doSomething(myVar, planItemInstance)}</pre>
</div>
</div>
<div class="paragraph">
<p>请注意，这些表达式支持解析基础数据类型（包括比较它们）、Bean、列表、数组和集合。</p>
</div>
<div class="paragraph">
<p>除了所有流程变量之外，还有一些可用于表达式的默认对象：</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>caseInstance</code>：<code>DelegateCaseInstance</code> 拥有有关正在进行的案例实例的额外信息。</p>
</li>
<li>
<p><code>planItemInstance</code>：<code>DelegatePlanItemInstance</code> 拥有有关当前方案条目的额外信息。</p>
</li>
</ul>
</div>
</div>
<div class="sect2">
<h3 id="cmmnExpressionsFunctions">3.7. 函数表达式</h3>
<div class="paragraph">
<p>[实验]表达式函数已在6.4.0版中添加。</p>
</div>
<div class="paragraph">
<p>为了更容易处理案例变量，在 <em>variables</em> 命名空间下可以使用一组开箱即用的函数。</p>
</div>
<div class="ulist">
<ul>
<li>
<p><strong>variables:get(varName)</strong>：检索变量的值。与直接在表达式中写入变量名称的主要区别在于，当变量不存在时，使用此函数不会抛出异常。例如，如果 myVariable 不存在，<em>${myVariable == "hello"}</em> 将会抛出异常，而 <em>${var:get(myVariable) == <em>hello</em>}</em> 将正常工作。</p>
</li>
<li>
<p><strong>variables:getOrDefault(varName, defaultValue)</strong>：与 <em>get(varName)</em> 类似，但可以选择提供默认值，该值在未设置变量或值为 <em>null</em> 时返回。</p>
</li>
<li>
<p><strong>variables:exists(varName)</strong>：如果变量具有非 null 值，则返回 <em>true</em> 。</p>
</li>
<li>
<p><strong>variables:isEmpty(varName)</strong> （别名 <em>:empty</em>）：检查变量值是否为空。 根据变量类型，行为如下：</p>
<div class="ulist">
<ul>
<li>
<p>对于 String 变量，如果变量是空字符串，则该变量被视为空。</p>
</li>
<li>
<p>对于 <code>java.util.Collection</code> 变量，如果集合没有元素，则返回 <em>true</em>。</p>
</li>
<li>
<p>对于 <code>ArrayNode</code> 变量，如果没有元素，则返回 <em>true</em></p>
</li>
<li>
<p>如果变量是 <em>null</em>，则始终返回 <em>true</em></p>
</li>
</ul>
</div>
</li>
<li>
<p><strong>variables:isNotEmpty(varName)</strong> （别名 <em>:notEmpty</em>）：<em>isEmpty(varName)</em> 的取反操作。</p>
</li>
<li>
<p><strong>variables:equals(varName, value)</strong>（别名_ <em>:eq</em>）：检查变量是否等于给定值。这是表达式的简写函数，否则将写为 <em>${execution.getVariable("varName") != null &amp;&amp; execution.getVariable("varName") == value}</em>。</p>
<div class="ulist">
<ul>
<li>
<p>如果变量值为 null，则返回 false（除非与 null 比较）。</p>
</li>
</ul>
</div>
</li>
<li>
<p><strong>variables:notEquals(varName, value)</strong>（别名 <em>:ne</em>）：<em>equals(varName, value)</em> 的取反操作。</p>
</li>
<li>
<p><strong>variables:contains(varName, value1, value2, &#8230;&#8203;)</strong>: 检查提供的<strong>所有</strong>值是否包含在变量中。根据变量类型，行为如下：</p>
<div class="ulist">
<ul>
<li>
<p>对于 String 变量，传递的值需要全部为变量一部分子字符串</p>
</li>
<li>
<p>对于 <code>java.util.Collection</code> 变量，所有传递的值都需要是集合的元素（常规 <em>contains</em> 语义）。</p>
</li>
<li>
<p>对于 <code>ArrayNode</code> 变量，支持检查 ArrayNode 是否包含作为变量类型支持的类型的 JsonNode</p>
</li>
<li>
<p>当变量值为 null 时，在所有情况下都返回 false。如果变量值不为 null，并且实例类型不是上述类型之一，则将返回 false。</p>
</li>
</ul>
</div>
</li>
<li>
<p><strong>variables:containsAny(varName, value1, value2, &#8230;&#8203;)</strong> ：类似于 <em>contains</em> 函数，但如果<strong>存在</strong>（不需要全部存在）传递的值包含在变量中，则返回 <em>true</em>。</p>
</li>
<li>
<p>比较函数：</p>
<div class="ulist">
<ul>
<li>
<p><strong>variables:lowerThan(varName, value)</strong>（别名 <em>:lessThan</em> 或 <em>:lt</em>）：<em>${execution.getVariable("varName") != null &amp;&amp; execution.getVariable("varName") &lt; value}</em> 的简写函数。</p>
</li>
<li>
<p><strong>variables:lowerThanOrEquals(varName, value)</strong>（别名 <em>:lessThanOrEquals</em> 或 <em>:lte</em>）：与上面的类似，相当于 <em>&lt; =</em></p>
</li>
<li>
<p><strong>variables:greaterThan(varName, value)</strong> （别名 <em>:gt</em>）：与上面的类似，相当于 <em>&gt;</em></p>
</li>
<li>
<p><strong>variables:greaterThanOrEquals(varName, value)</strong> （别名 <em>:gte</em>）：与上面的类似，相当于 <em>&gt; =</em></p>
</li>
</ul>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p><em>variables</em> 命名空间的别名为 <em>vars</em> 或 <em>var</em>。因此 <em>variables:get(varName)</em> 等同于使用 <em>vars:get(varName)</em> 或 <em>var:get(varName)</em>。请注意，不需要再次在变量名称周围加上引号：<em>var:get(varName)</em> 等同于 <em>var:get('varName')</em> 或 <em>var:get("varName")</em>。</p>
</div>
<div class="paragraph">
<p>另请注意，在上述任何函数中，都不需要将 <em>planItemInstance</em> 或 <em>caseInstance</em> 传递给函数（它们在不使用函数时需要传递）。在调用函数时，引擎将注入适当的变量作用域。这也意味着在 BPMN 流程定义中编写表达式时，可以以完全相同的方式使用这些函数。</p>
</div>
<div class="paragraph">
<p>这些变量函数的使用在 CMMN 中尤其有用，例如在写入 if 部分的哨兵条件时，采用以下 CMMN 案例定义：</p>
</div>
<div class="imageblock" style="text-align: center">
<div class="content">
<img src="images/cmmn.expression-functions.png" alt="cmmn.expression functions">
</div>
</div>
<div class="paragraph">
<p>Assume the sentry has an if-part besides the completion event. Right after a case instance is started, this if-part condition will be evaluated (as the stage becomes available). If the condition is of the form <em>${someVariable == someValue}</em>, this means the variable needs to be available when starting the case instance. In many cases, this is not possible or the variable comes later (e.g. from a form), which leads to a low-level <em>PropertyNotFoundException</em>. Taking the potential nullability in account, the correct expression would have to be:
假设哨兵除了完成事件之外还有一个 if 部分。在启动案例实例后，将评估此 if 部分条件（当此阶段变为可用时）。如果条件的形式为 <em>${someVariable == someValue}</em>，则表示该变量在启动案例实例时就将可用。在许多情况下，这是不可能的，或者变量稍后才会出现 （例如来自一个表单），这会导致一个低级的 <em>PropertyNotFoundException</em>。考虑到潜在的可空性，正确的表达必须是：</p>
</div>
<div class="listingblock">
<div class="content">
<pre>${planItemInstance.getVariable('someVariable') != null &amp;&amp; planItemInstance.getVariable('someVariable') == someValue}</pre>
</div>
</div>
<div class="paragraph">
<p>这很长。但是，使用上述功能可以简化为</p>
</div>
<div class="listingblock">
<div class="content">
<pre>${var:eq(someVariable, someValue)}</pre>
</div>
</div>
<div class="paragraph">
<p>或者</p>
</div>
<div class="listingblock">
<div class="content">
<pre>${var:get(someVariable) == someValue}</pre>
</div>
</div>
<div class="paragraph">
<p>函数实现考虑了变量的可空性（并且在变量为 null 的情况下不抛出异常）并且将正确地处理相等性。</p>
</div>
<div class="paragraph">
<p>此外，可以注册自定义函数在表达式中使用。有关更多信息，请参阅 <code>org.flowable.common.engine.api.delegate.FlowableFunctionDelegate</code> 接口。</p>
</div>
</div>
<div class="sect2">
<h3 id="apiUnitTesting">3.8. 单元测试</h3>
<div class="paragraph">
<p>案例是软件项目不可或缺的一部分，它们应该以与测试正常应用程序逻辑相同的方式进行测试：使用单元测试。
由于 Flowable 是一个嵌入式的 Java 引擎，因此为业务案例编写单元测试就像编写常规单元测试一样简单。</p>
</div>
<div class="paragraph">
<p>Flowable 支持 JUnit 4、JUnit 5 做单元测试。</p>
</div>
<div class="paragraph">
<p>在 JUnit 5中，需要使用 <code>org.flowable.cmmn.engine.test.FlowableCmmnTest</code> 注解或手动注册 <code>org.flowable.cmmn.engine.test.FlowableCmmnExtension</code>。
<code>FlowableCmmnTest</code> 注释是一个元注释，实现了对 <code>FlowableCmmnExtension</code> 的注册（即它实现了 <code>@ExtendWith(FlowableCmmnExtension.class)</code>）。
这将使 CmmnEngine 和其服务类可用作测试和生命周期方法的参数（<code>@BeforeAll</code>、<code>@BeforeEach</code>、<code>@AfterEach</code>、<code>@AfterAll</code>）。
在每次测试之前，默认会使用类路径上的 <code>flowable.cmmn.cfg.xml</code> 配置文件初始化 CmmnEngine。
为了指定不同的配置文件，需要使用 <code>org.flowable.cmmn.engine.test.CmmnConfigurationResource</code> 注解（参见第二个示例）。
使用相同的配置文件时，CMMN 引擎会在多个单元测试之间静态缓存。</p>
</div>
<div class="paragraph">
<p>通过使用 <code>FlowableCmmnExtension</code>，您可以对测试方法使用 <code>org.flowable.cmmn.engine.test.CmmnDeployment</code> 注解。
当使用有 <code>@CmmnDeployment</code> 注解的测试方法时，在每次测试之前，将会发布在 <code>CmmnDeployment#resources</code> 下定义的 cmmn 文件。
如果没有定义资源，将发布与测试类在同一包中的 <code>testClassName.testMethod.cmmn</code> 形式的资源文件。
在测试结束时，部署文件将会删除，包括所有相关的案例实例、定义等。
有关更多信息，请参阅 <code>CmmnDeployment</code> 类。</p>
</div>
<div class="paragraph">
<p>考虑到所有这些，JUnit 5单元测试看起来如下：</p>
</div>
<div class="listingblock">
<div class="title">使用默认配置文件的 Junit 5 单元测试</div>
<div class="content">
<pre class="pygments highlight"><code data-lang="java"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv"> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30</div></td><td class="code"><span></span><span class="tok-nd">@FlowableCmmnTest</span>
<span class="tok-kd">class</span> <span class="tok-nc">MyTest</span> <span class="tok-o">{</span>

  <span class="tok-kd">private</span> <span class="tok-n">CmmnEngine</span> <span class="tok-n">cmmnEngine</span><span class="tok-o">;</span>
  <span class="tok-kd">private</span> <span class="tok-n">CmmnRuntimeService</span> <span class="tok-n">cmmnRuntimeService</span><span class="tok-o">;</span>
  <span class="tok-kd">private</span> <span class="tok-n">CmmnTaskService</span> <span class="tok-n">cmmnTaskService</span><span class="tok-o">;</span>

  <span class="tok-nd">@BeforeEach</span>
  <span class="tok-kt">void</span> <span class="tok-nf">setUp</span><span class="tok-o">(</span><span class="tok-n">CmmnEngine</span> <span class="tok-n">cmmnEngine</span><span class="tok-o">)</span> <span class="tok-o">{</span>
    <span class="tok-k">this</span><span class="tok-o">.</span><span class="tok-na">cmmnEngine</span> <span class="tok-o">=</span> <span class="tok-n">cmmnEngine</span><span class="tok-o">;</span>
    <span class="tok-k">this</span><span class="tok-o">.</span><span class="tok-na">cmmnRuntimeService</span> <span class="tok-o">=</span> <span class="tok-n">cmmnEngine</span><span class="tok-o">.</span><span class="tok-na">getCmmnRuntimeService</span><span class="tok-o">();</span>
    <span class="tok-k">this</span><span class="tok-o">.</span><span class="tok-na">cmmnTaskService</span> <span class="tok-o">=</span> <span class="tok-n">cmmnEngine</span><span class="tok-o">.</span><span class="tok-na">getTaskRuntimeService</span><span class="tok-o">();</span>
  <span class="tok-o">}</span>

  <span class="tok-nd">@Test</span>
  <span class="tok-nd">@CmmnDeployment</span>
  <span class="tok-kt">void</span> <span class="tok-nf">testSingleHumanTask</span><span class="tok-o">()</span> <span class="tok-o">{</span>
    <span class="tok-n">CaseInstance</span> <span class="tok-n">caseInstance</span> <span class="tok-o">=</span> <span class="tok-n">cmmnRuntimeService</span><span class="tok-o">.</span><span class="tok-na">createCaseInstanceBuilder</span><span class="tok-o">()</span>
                    <span class="tok-o">.</span><span class="tok-na">caseDefinitionKey</span><span class="tok-o">(</span><span class="tok-s">&quot;myCase&quot;</span><span class="tok-o">)</span>
                    <span class="tok-o">.</span><span class="tok-na">start</span><span class="tok-o">();</span>
	<span class="tok-n">assertNotNull</span><span class="tok-o">(</span><span class="tok-n">caseInstance</span><span class="tok-o">);</span>

    <span class="tok-n">Task</span> <span class="tok-n">task</span> <span class="tok-o">=</span> <span class="tok-n">cmmnTaskService</span><span class="tok-o">.</span><span class="tok-na">createTaskQuery</span><span class="tok-o">().</span><span class="tok-na">caseInstanceId</span><span class="tok-o">(</span><span class="tok-n">caseInstance</span><span class="tok-o">.</span><span class="tok-na">getId</span><span class="tok-o">()).</span><span class="tok-na">singleResult</span><span class="tok-o">();</span>
    <span class="tok-n">assertEquals</span><span class="tok-o">(</span><span class="tok-s">&quot;Task 1&quot;</span><span class="tok-o">,</span> <span class="tok-n">task</span><span class="tok-o">.</span><span class="tok-na">getName</span><span class="tok-o">());</span>
    <span class="tok-n">assertEquals</span><span class="tok-o">(</span><span class="tok-s">&quot;JohnDoe&quot;</span><span class="tok-o">,</span> <span class="tok-n">task</span><span class="tok-o">.</span><span class="tok-na">getAssignee</span><span class="tok-o">());</span>

    <span class="tok-n">cmmnTaskService</span><span class="tok-o">.</span><span class="tok-na">complete</span><span class="tok-o">(</span><span class="tok-n">task</span><span class="tok-o">.</span><span class="tok-na">getId</span><span class="tok-o">());</span>
    <span class="tok-n">assertEquals</span><span class="tok-o">(</span><span class="tok-mi">0</span><span class="tok-o">,</span> <span class="tok-n">cmmnRuntimeService</span><span class="tok-o">.</span><span class="tok-na">createCaseInstanceQuery</span><span class="tok-o">().</span><span class="tok-na">count</span><span class="tok-o">());</span>
  <span class="tok-o">}</span>
<span class="tok-o">}</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="listingblock">
<div class="content">
<pre>使用 JUnit 5，您还可以将部署文件的 ID（使用 org.flowable.cmmn.engine.test.CmmnDeploymentId）注入到测试和生命周期方法中。</pre>
</div>
</div>
<div class="listingblock">
<div class="title">使用自定义配置文件的Junit 5单元测试</div>
<div class="content">
<pre class="pygments highlight"><code data-lang="java"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv"> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30</div></td><td class="code"><span></span><span class="tok-nd">@FlowableCmmnTest</span>
<span class="tok-nd">@CmmnConfigurationResource</span><span class="tok-o">(</span><span class="tok-s">&quot;flowable.custom.cmmn.cfg.xml&quot;</span><span class="tok-o">)</span>
<span class="tok-kd">class</span> <span class="tok-nc">MyTest</span> <span class="tok-o">{</span>

  <span class="tok-kd">private</span> <span class="tok-n">CmmnEngine</span> <span class="tok-n">cmmnEngine</span><span class="tok-o">;</span>
  <span class="tok-kd">private</span> <span class="tok-n">CmmnRuntimeService</span> <span class="tok-n">cmmnRuntimeService</span><span class="tok-o">;</span>
  <span class="tok-kd">private</span> <span class="tok-n">CmmnTaskService</span> <span class="tok-n">cmmnTaskService</span><span class="tok-o">;</span>

  <span class="tok-nd">@BeforeEach</span>
  <span class="tok-kt">void</span> <span class="tok-nf">setUp</span><span class="tok-o">(</span><span class="tok-n">CmmnEngine</span> <span class="tok-n">cmmnEngine</span><span class="tok-o">)</span> <span class="tok-o">{</span>
    <span class="tok-k">this</span><span class="tok-o">.</span><span class="tok-na">cmmnEngine</span> <span class="tok-o">=</span> <span class="tok-n">cmmnEngine</span><span class="tok-o">;</span>
    <span class="tok-k">this</span><span class="tok-o">.</span><span class="tok-na">cmmnRuntimeService</span> <span class="tok-o">=</span> <span class="tok-n">cmmnEngine</span><span class="tok-o">.</span><span class="tok-na">getCmmnRuntimeService</span><span class="tok-o">();</span>
    <span class="tok-k">this</span><span class="tok-o">.</span><span class="tok-na">cmmnTaskService</span> <span class="tok-o">=</span> <span class="tok-n">cmmnEngine</span><span class="tok-o">.</span><span class="tok-na">getTaskRuntimeService</span><span class="tok-o">();</span>
  <span class="tok-o">}</span>
  <span class="tok-nd">@Test</span>
  <span class="tok-nd">@CmmnDeployment</span>
  <span class="tok-kt">void</span> <span class="tok-nf">testSingleHumanTask</span><span class="tok-o">()</span> <span class="tok-o">{</span>
    <span class="tok-n">CaseInstance</span> <span class="tok-n">caseInstance</span> <span class="tok-o">=</span> <span class="tok-n">cmmnRuntimeService</span><span class="tok-o">.</span><span class="tok-na">createCaseInstanceBuilder</span><span class="tok-o">()</span>
                    <span class="tok-o">.</span><span class="tok-na">caseDefinitionKey</span><span class="tok-o">(</span><span class="tok-s">&quot;myCase&quot;</span><span class="tok-o">)</span>
                    <span class="tok-o">.</span><span class="tok-na">start</span><span class="tok-o">();</span>
	<span class="tok-n">assertNotNull</span><span class="tok-o">(</span><span class="tok-n">caseInstance</span><span class="tok-o">);</span>

    <span class="tok-n">Task</span> <span class="tok-n">task</span> <span class="tok-o">=</span> <span class="tok-n">cmmnTaskService</span><span class="tok-o">.</span><span class="tok-na">createTaskQuery</span><span class="tok-o">().</span><span class="tok-na">caseInstanceId</span><span class="tok-o">(</span><span class="tok-n">caseInstance</span><span class="tok-o">.</span><span class="tok-na">getId</span><span class="tok-o">()).</span><span class="tok-na">singleResult</span><span class="tok-o">();</span>
    <span class="tok-n">assertEquals</span><span class="tok-o">(</span><span class="tok-s">&quot;Task 1&quot;</span><span class="tok-o">,</span> <span class="tok-n">task</span><span class="tok-o">.</span><span class="tok-na">getName</span><span class="tok-o">());</span>
    <span class="tok-n">assertEquals</span><span class="tok-o">(</span><span class="tok-s">&quot;JohnDoe&quot;</span><span class="tok-o">,</span> <span class="tok-n">task</span><span class="tok-o">.</span><span class="tok-na">getAssignee</span><span class="tok-o">());</span>

    <span class="tok-n">cmmnTaskService</span><span class="tok-o">.</span><span class="tok-na">complete</span><span class="tok-o">(</span><span class="tok-n">task</span><span class="tok-o">.</span><span class="tok-na">getId</span><span class="tok-o">());</span>
    <span class="tok-n">assertEquals</span><span class="tok-o">(</span><span class="tok-mi">0</span><span class="tok-o">,</span> <span class="tok-n">cmmnRuntimeService</span><span class="tok-o">.</span><span class="tok-na">createCaseInstanceQuery</span><span class="tok-o">().</span><span class="tok-na">count</span><span class="tok-o">());</span>
  <span class="tok-o">}</span>
<span class="tok-o">}</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>在 JUnit 4 中，<em>org.flowable.cmmn.engine.test.FlowableCmmnTestCase</em> 可用作父类。它默认使用 <em>flowable.cmmn.cfg.xml</em> 配置文件，如果缺少此类文件，则使用连接 H2 内存数据库的标准 CmmnEngine。
在后台，CmmnTestRunner 用于初始化 CMMN 引擎。请注意下面的示例中如何使用 <em>@CmmnDeployment</em> 注解自动部署案例定义（它将在与测试类相同的文件夹中查找 .cmmn 文件，并期望文件名为&lt;测试类名&gt;.&lt;测试方法名称&gt;.cmmn）。</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="java"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv"> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19</div></td><td class="code"><span></span>
<span class="tok-kd">public</span> <span class="tok-kd">class</span> <span class="tok-nc">MyTest</span> <span class="tok-kd">extends</span> <span class="tok-n">FlowableCmmnTestCase</span> <span class="tok-o">{</span>

  <span class="tok-nd">@Test</span>
  <span class="tok-nd">@CmmnDeployment</span>
  <span class="tok-kd">public</span> <span class="tok-kt">void</span> <span class="tok-nf">testSingleHumanTask</span><span class="tok-o">()</span> <span class="tok-o">{</span>
    <span class="tok-n">CaseInstance</span> <span class="tok-n">caseInstance</span> <span class="tok-o">=</span> <span class="tok-n">cmmnRuntimeService</span><span class="tok-o">.</span><span class="tok-na">createCaseInstanceBuilder</span><span class="tok-o">()</span>
                    <span class="tok-o">.</span><span class="tok-na">caseDefinitionKey</span><span class="tok-o">(</span><span class="tok-s">&quot;myCase&quot;</span><span class="tok-o">)</span>
                    <span class="tok-o">.</span><span class="tok-na">start</span><span class="tok-o">();</span>
	<span class="tok-n">assertNotNull</span><span class="tok-o">(</span><span class="tok-n">caseInstance</span><span class="tok-o">);</span>

    <span class="tok-n">Task</span> <span class="tok-n">task</span> <span class="tok-o">=</span> <span class="tok-n">cmmnTaskService</span><span class="tok-o">.</span><span class="tok-na">createTaskQuery</span><span class="tok-o">().</span><span class="tok-na">caseInstanceId</span><span class="tok-o">(</span><span class="tok-n">caseInstance</span><span class="tok-o">.</span><span class="tok-na">getId</span><span class="tok-o">()).</span><span class="tok-na">singleResult</span><span class="tok-o">();</span>
    <span class="tok-n">assertEquals</span><span class="tok-o">(</span><span class="tok-s">&quot;Task 1&quot;</span><span class="tok-o">,</span> <span class="tok-n">task</span><span class="tok-o">.</span><span class="tok-na">getName</span><span class="tok-o">());</span>
    <span class="tok-n">assertEquals</span><span class="tok-o">(</span><span class="tok-s">&quot;JohnDoe&quot;</span><span class="tok-o">,</span> <span class="tok-n">task</span><span class="tok-o">.</span><span class="tok-na">getAssignee</span><span class="tok-o">());</span>

    <span class="tok-n">cmmnTaskService</span><span class="tok-o">.</span><span class="tok-na">complete</span><span class="tok-o">(</span><span class="tok-n">task</span><span class="tok-o">.</span><span class="tok-na">getId</span><span class="tok-o">());</span>
    <span class="tok-n">assertEquals</span><span class="tok-o">(</span><span class="tok-mi">0</span><span class="tok-o">,</span> <span class="tok-n">cmmnRuntimeService</span><span class="tok-o">.</span><span class="tok-na">createCaseInstanceQuery</span><span class="tok-o">().</span><span class="tok-na">count</span><span class="tok-o">());</span>
  <span class="tok-o">}</span>
<span class="tok-o">}</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>此外，使用 <em>FlowableCmmnRule</em> 并允许设置自定义配置：</p>
</div>
<div class="listingblock">
<div class="title">规则相关的 JUnit 4 单元测试</div>
<div class="content">
<pre class="pygments highlight"><code data-lang="java"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv"> 1
 2
 3
 4
 5
 6
 7
 8
 9
10</div></td><td class="code"><span></span><span class="tok-nd">@Rule</span>
<span class="tok-kd">public</span> <span class="tok-n">FlowableCmmnRule</span> <span class="tok-n">cmmnRule</span> <span class="tok-o">=</span> <span class="tok-k">new</span> <span class="tok-n">FlowableCmmnRule</span><span class="tok-o">(</span><span class="tok-s">&quot;org/flowable/custom.cfg.xml&quot;</span><span class="tok-o">)</span>

<span class="tok-nd">@Test</span>
<span class="tok-nd">@CmmnDeployment</span>
<span class="tok-kd">public</span> <span class="tok-kt">void</span> <span class="tok-nf">testSomething</span><span class="tok-o">()</span> <span class="tok-o">{</span>
    <span class="tok-c1">// ...</span>
    <span class="tok-n">assertThat</span><span class="tok-o">((</span><span class="tok-n">String</span><span class="tok-o">)</span> <span class="tok-n">cmmnRule</span><span class="tok-o">.</span><span class="tok-na">getCmmnRuntimeService</span><span class="tok-o">().</span><span class="tok-na">getVariable</span><span class="tok-o">(</span><span class="tok-n">caseInstance</span><span class="tok-o">.</span><span class="tok-na">getId</span><span class="tok-o">(),</span> <span class="tok-s">&quot;test&quot;</span><span class="tok-o">),</span> <span class="tok-n">containsString</span><span class="tok-o">(</span><span class="tok-s">&quot;John&quot;</span><span class="tok-o">));</span>
    <span class="tok-c1">// ...</span>
<span class="tok-o">}</span>
</td></tr></table></code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="springintegration">4. 整合Spring</h2>
<div class="sectionbody">
<div class="paragraph">
<p>不使用spring也可以很好的使用Flowable,在这一章,我们会介绍一些很有用的整合特性。</p>
</div>
<div class="sect2">
<h3 id="_cmmnenginefactorybean">4.1. CmmnEngineFactoryBean</h3>
<div class="paragraph">
<p><code>CmmnEngine</code> 可以配置位一般的spring bean。整合的基点是 <code>org.flowable.cmmn.spring.CmmnEngineFactoryBean</code> 类。这个bean加载CMMN配置文件并创建CMMN引擎。这意味着创建和配置和之前文档 <a href="#configuration">configuration section</a> 是一致的。整合spring,配置文件和引擎的服务类看上去是这样的：</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="xml"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1
2
3
4
5
6
7</div></td><td class="code"><span></span><span class="tok-nt">&lt;bean</span> <span class="tok-na">id=</span><span class="tok-s">&quot;cmmnEngineConfiguration&quot;</span> <span class="tok-na">class=</span><span class="tok-s">&quot;org.flowable.cmmn.spring.SpringCmmnEngineConfiguration&quot;</span><span class="tok-nt">&gt;</span>
    ...
<span class="tok-nt">&lt;/bean&gt;</span>

<span class="tok-nt">&lt;bean</span> <span class="tok-na">id=</span><span class="tok-s">&quot;cmmnEngine&quot;</span> <span class="tok-na">class=</span><span class="tok-s">&quot;org.flowable.cmmn.spring.CmmnEngineFactoryBean&quot;</span><span class="tok-nt">&gt;</span>
  <span class="tok-nt">&lt;property</span> <span class="tok-na">name=</span><span class="tok-s">&quot;cmmnEngineConfiguration&quot;</span> <span class="tok-na">ref=</span><span class="tok-s">&quot;cmmnEngineConfiguration&quot;</span> <span class="tok-nt">/&gt;</span>
<span class="tok-nt">&lt;/bean&gt;</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>注意 <code>cmmnEngineConfiguration</code> bean现在使用的是 <code>org.flowable.cmmn.spring.SpringCmmnEngineConfiguration</code> 。</p>
</div>
</div>
<div class="sect2">
<h3 id="_默认spring配置">4.2. 默认spring配置</h3>
<div class="paragraph">
<p>以下章节包括数据源(dataSource),事务管理(transactionManager),cmmn引擎(cmmnEngine)以及Flowable引擎各服务。</p>
</div>
<div class="paragraph">
<p>当将数据源(DataSource)装入 <code>SpringCmmnEngineConfiguration</code> (使用字段"dataSource") 里,Flowable将全局使用  <code>org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy</code> 代理,用来包装传入的数据源。目的是让spring事务很好的管理数据库连接。这意味着您在spring配置里,不需要再代理数据源。尽管也可以将一个  <code>TransactionAwareDataSourceProxy</code> 装入 <code>SpringCmmnEngineConfiguration</code> ,但是再这种情况下,CMMN引擎不会再次包装该数据源。</p>
</div>
<div class="paragraph">
<p><strong>确保在使用在spring配置中声明 <code>TransactionAwareDataSourceProxy</code> 时,不要使用在spring事务代管下的数据源（比如 DataSourceTransactionManager 需要未被代理的dataSource)。</strong></p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="xml"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv"> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39</div></td><td class="code"><span></span><span class="tok-nt">&lt;beans</span> <span class="tok-na">xmlns=</span><span class="tok-s">&quot;http://www.springframework.org/schema/beans&quot;</span>
       <span class="tok-na">xmlns:context=</span><span class="tok-s">&quot;http://www.springframework.org/schema/context&quot;</span>
       <span class="tok-na">xmlns:tx=</span><span class="tok-s">&quot;http://www.springframework.org/schema/tx&quot;</span>
       <span class="tok-na">xmlns:xsi=</span><span class="tok-s">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span>
       <span class="tok-na">xsi:schemaLocation=</span><span class="tok-s">&quot;http://www.springframework.org/schema/beans</span>
<span class="tok-s">                             http://www.springframework.org/schema/beans/spring-beans.xsd</span>
<span class="tok-s">                           http://www.springframework.org/schema/context</span>
<span class="tok-s">                             http://www.springframework.org/schema/context/spring-context-2.5.xsd</span>
<span class="tok-s">                           http://www.springframework.org/schema/tx</span>
<span class="tok-s">                             http://www.springframework.org/schema/tx/spring-tx-3.0.xsd&quot;</span><span class="tok-nt">&gt;</span>

  <span class="tok-nt">&lt;bean</span> <span class="tok-na">id=</span><span class="tok-s">&quot;dataSource&quot;</span> <span class="tok-na">class=</span><span class="tok-s">&quot;org.springframework.jdbc.datasource.SimpleDriverDataSource&quot;</span><span class="tok-nt">&gt;</span>
    <span class="tok-nt">&lt;property</span> <span class="tok-na">name=</span><span class="tok-s">&quot;driverClass&quot;</span> <span class="tok-na">value=</span><span class="tok-s">&quot;org.h2.Driver&quot;</span> <span class="tok-nt">/&gt;</span>
    <span class="tok-nt">&lt;property</span> <span class="tok-na">name=</span><span class="tok-s">&quot;url&quot;</span> <span class="tok-na">value=</span><span class="tok-s">&quot;jdbc:h2:mem:flowable;DB_CLOSE_DELAY=1000&quot;</span> <span class="tok-nt">/&gt;</span>
    <span class="tok-nt">&lt;property</span> <span class="tok-na">name=</span><span class="tok-s">&quot;username&quot;</span> <span class="tok-na">value=</span><span class="tok-s">&quot;sa&quot;</span> <span class="tok-nt">/&gt;</span>
    <span class="tok-nt">&lt;property</span> <span class="tok-na">name=</span><span class="tok-s">&quot;password&quot;</span> <span class="tok-na">value=</span><span class="tok-s">&quot;&quot;</span> <span class="tok-nt">/&gt;</span>
  <span class="tok-nt">&lt;/bean&gt;</span>

  <span class="tok-nt">&lt;bean</span> <span class="tok-na">id=</span><span class="tok-s">&quot;transactionManager&quot;</span> <span class="tok-na">class=</span><span class="tok-s">&quot;org.springframework.jdbc.datasource.DataSourceTransactionManager&quot;</span><span class="tok-nt">&gt;</span>
    <span class="tok-nt">&lt;property</span> <span class="tok-na">name=</span><span class="tok-s">&quot;dataSource&quot;</span> <span class="tok-na">ref=</span><span class="tok-s">&quot;dataSource&quot;</span> <span class="tok-nt">/&gt;</span>
  <span class="tok-nt">&lt;/bean&gt;</span>

  <span class="tok-nt">&lt;bean</span> <span class="tok-na">id=</span><span class="tok-s">&quot;cmmnEngineConfiguration&quot;</span> <span class="tok-na">class=</span><span class="tok-s">&quot;org.flowable.cmmn.spring.SpringCmmnEngineConfiguration&quot;</span><span class="tok-nt">&gt;</span>
    <span class="tok-nt">&lt;property</span> <span class="tok-na">name=</span><span class="tok-s">&quot;dataSource&quot;</span> <span class="tok-na">ref=</span><span class="tok-s">&quot;dataSource&quot;</span> <span class="tok-nt">/&gt;</span>
    <span class="tok-nt">&lt;property</span> <span class="tok-na">name=</span><span class="tok-s">&quot;transactionManager&quot;</span> <span class="tok-na">ref=</span><span class="tok-s">&quot;transactionManager&quot;</span> <span class="tok-nt">/&gt;</span>
    <span class="tok-nt">&lt;property</span> <span class="tok-na">name=</span><span class="tok-s">&quot;databaseSchemaUpdate&quot;</span> <span class="tok-na">value=</span><span class="tok-s">&quot;true&quot;</span> <span class="tok-nt">/&gt;</span>
  <span class="tok-nt">&lt;/bean&gt;</span>

  <span class="tok-nt">&lt;bean</span> <span class="tok-na">id=</span><span class="tok-s">&quot;cmmnEngine&quot;</span> <span class="tok-na">class=</span><span class="tok-s">&quot;org.flowable.cmmn.spring.CmmnEngineFactoryBean&quot;</span><span class="tok-nt">&gt;</span>
    <span class="tok-nt">&lt;property</span> <span class="tok-na">name=</span><span class="tok-s">&quot;cmmnEngineConfiguration&quot;</span> <span class="tok-na">ref=</span><span class="tok-s">&quot;cmmnEngineConfiguration&quot;</span> <span class="tok-nt">/&gt;</span>
  <span class="tok-nt">&lt;/bean&gt;</span>

  <span class="tok-nt">&lt;bean</span> <span class="tok-na">id=</span><span class="tok-s">&quot;cmmnRepositoryService&quot;</span> <span class="tok-na">factory-bean=</span><span class="tok-s">&quot;cmmnEngine&quot;</span> <span class="tok-na">factory-method=</span><span class="tok-s">&quot;getCmmnRepositoryService&quot;</span> <span class="tok-nt">/&gt;</span>
  <span class="tok-nt">&lt;bean</span> <span class="tok-na">id=</span><span class="tok-s">&quot;cmmnRuntimeService&quot;</span> <span class="tok-na">factory-bean=</span><span class="tok-s">&quot;cmmnEngine&quot;</span> <span class="tok-na">factory-method=</span><span class="tok-s">&quot;getCmmnRuntimeService&quot;</span> <span class="tok-nt">/&gt;</span>
  <span class="tok-nt">&lt;bean</span> <span class="tok-na">id=</span><span class="tok-s">&quot;cmmnTaskService&quot;</span> <span class="tok-na">factory-bean=</span><span class="tok-s">&quot;cmmnEngine&quot;</span> <span class="tok-na">factory-method=</span><span class="tok-s">&quot;getCmmnTaskService&quot;</span> <span class="tok-nt">/&gt;</span>
  <span class="tok-nt">&lt;bean</span> <span class="tok-na">id=</span><span class="tok-s">&quot;cmmnHistoryService&quot;</span> <span class="tok-na">factory-bean=</span><span class="tok-s">&quot;cmmnEngine&quot;</span> <span class="tok-na">factory-method=</span><span class="tok-s">&quot;getCmmnHistoryService&quot;</span> <span class="tok-nt">/&gt;</span>
  <span class="tok-nt">&lt;bean</span> <span class="tok-na">id=</span><span class="tok-s">&quot;cmmnManagementService&quot;</span> <span class="tok-na">factory-bean=</span><span class="tok-s">&quot;cmmnEngine&quot;</span> <span class="tok-na">factory-method=</span><span class="tok-s">&quot;getCmmnManagementService&quot;</span> <span class="tok-nt">/&gt;</span>

...
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>首先,application context(spring容器上下文)可以使用任何spring的配置方式(比如：xml,@Configuration等)。这个示例使用的XML配置方式。</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="java"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1
2</div></td><td class="code"><span></span><span class="tok-n">ClassPathXmlApplicationContext</span> <span class="tok-n">applicationContext</span> <span class="tok-o">=</span> <span class="tok-k">new</span> <span class="tok-n">ClassPathXmlApplicationContext</span><span class="tok-o">(</span>
	<span class="tok-s">&quot;org/flowable/cmmn/examples/spring/SpringIntegrationTest-context.xml&quot;</span><span class="tok-o">);</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>测试时这样使用：</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="java"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1
2</div></td><td class="code"><span></span><span class="tok-nd">@ContextConfiguration</span><span class="tok-o">(</span>
  <span class="tok-s">&quot;classpath:org/flowable/cmmn/spring/test/SpringIntegrationTest-context.xml&quot;</span><span class="tok-o">)</span>
</td></tr></table></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="springExpressions">4.3. 表达式</h3>
<div class="paragraph">
<p>使用CmmnEngineFactoryBean,所有的spring bean在CMMN流程中的表达式默认都是可见的。可以在配置SpringCmmnEngineConfiguration的beans(类型Map)属性,限制表达式中使用的bean。下面的示例是暴露了单独的名为printer的bean。
* 如果没有需要暴露给表达式的bean,配置 beans属性位一个空map。当不设置 <em>beans</em> 属性时,所有spring容器里的bean都是可用的。 *</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="xml"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv"> 1
 2
 3
 4
 5
 6
 7
 8
 9
10</div></td><td class="code"><span></span><span class="tok-nt">&lt;bean</span> <span class="tok-na">id=</span><span class="tok-s">&quot;cmmnEngineConfiguration&quot;</span> <span class="tok-na">class=</span><span class="tok-s">&quot;org.flowable.cmmn.spring.SpringCmmnEngineConfiguration&quot;</span><span class="tok-nt">&gt;</span>
  ...
  <span class="tok-nt">&lt;property</span> <span class="tok-na">name=</span><span class="tok-s">&quot;beans&quot;</span><span class="tok-nt">&gt;</span>
    <span class="tok-nt">&lt;map&gt;</span>
      <span class="tok-nt">&lt;entry</span> <span class="tok-na">key=</span><span class="tok-s">&quot;printer&quot;</span> <span class="tok-na">value-ref=</span><span class="tok-s">&quot;printer&quot;</span> <span class="tok-nt">/&gt;</span>
    <span class="tok-nt">&lt;/map&gt;</span>
  <span class="tok-nt">&lt;/property&gt;</span>
<span class="tok-nt">&lt;/bean&gt;</span>

<span class="tok-nt">&lt;bean</span> <span class="tok-na">id=</span><span class="tok-s">&quot;printer&quot;</span> <span class="tok-na">class=</span><span class="tok-s">&quot;org.flowable.cmmn.examples.spring.Printer&quot;</span> <span class="tok-nt">/&gt;</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>将可以使用的bean暴露给表达式：</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="xml"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv"> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20</div></td><td class="code"><span></span>...
	<span class="tok-nt">&lt;case</span> <span class="tok-na">id=</span><span class="tok-s">&quot;myCase&quot;</span><span class="tok-nt">&gt;</span>
        <span class="tok-nt">&lt;casePlanModel</span> <span class="tok-na">id=</span><span class="tok-s">&quot;myPlanModel&quot;</span> <span class="tok-na">name=</span><span class="tok-s">&quot;My CasePlanModel&quot;</span><span class="tok-nt">&gt;</span>

            <span class="tok-nt">&lt;planItem</span> <span class="tok-na">id=</span><span class="tok-s">&quot;planItem1&quot;</span> <span class="tok-na">name=</span><span class="tok-s">&quot;Task One&quot;</span> <span class="tok-na">definitionRef=</span><span class="tok-s">&quot;serviceTask&quot;</span> <span class="tok-nt">/&gt;</span>
            <span class="tok-nt">&lt;planItem</span> <span class="tok-na">id=</span><span class="tok-s">&quot;planItem2&quot;</span> <span class="tok-na">name=</span><span class="tok-s">&quot;The Case&quot;</span> <span class="tok-na">definitionRef=</span><span class="tok-s">&quot;task&quot;</span><span class="tok-nt">&gt;</span>
                <span class="tok-nt">&lt;entryCriterion</span> <span class="tok-na">sentryRef=</span><span class="tok-s">&quot;sentry1&quot;</span> <span class="tok-nt">/&gt;</span>
            <span class="tok-nt">&lt;/planItem&gt;</span>

            <span class="tok-nt">&lt;sentry</span> <span class="tok-na">id=</span><span class="tok-s">&quot;sentry1&quot;</span><span class="tok-nt">&gt;</span>
                <span class="tok-nt">&lt;planItemOnPart</span> <span class="tok-na">sourceRef=</span><span class="tok-s">&quot;planItem1&quot;</span><span class="tok-nt">&gt;</span>
                    <span class="tok-nt">&lt;standardEvent&gt;</span>complete<span class="tok-nt">&lt;/standardEvent&gt;</span>
                <span class="tok-nt">&lt;/planItemOnPart&gt;</span>
            <span class="tok-nt">&lt;/sentry&gt;</span>

            <span class="tok-nt">&lt;task</span> <span class="tok-na">id=</span><span class="tok-s">&quot;serviceTask&quot;</span> <span class="tok-na">flowable:type=</span><span class="tok-s">&quot;java&quot;</span> <span class="tok-na">flowable:expression=</span><span class="tok-s">&quot;${printer.printMessage(var1)}&quot;</span> <span class="tok-na">flowable:resultVariableName=</span><span class="tok-s">&quot;customResponse&quot;</span> <span class="tok-nt">/&gt;</span>
            <span class="tok-nt">&lt;task</span> <span class="tok-na">id=</span><span class="tok-s">&quot;task&quot;</span> <span class="tok-na">name=</span><span class="tok-s">&quot;The Task&quot;</span> <span class="tok-na">isBlocking=</span><span class="tok-s">&quot;true&quot;</span> <span class="tok-nt">/&gt;</span>

        <span class="tok-nt">&lt;/casePlanModel&gt;</span>
    <span class="tok-nt">&lt;/case&gt;</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="literalblock">
<div class="content">
<pre>+Printer+ 代码：</pre>
</div>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="java"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1
2
3
4
5
6</div></td><td class="code"><span></span><span class="tok-kd">public</span> <span class="tok-kd">class</span> <span class="tok-nc">Printer</span> <span class="tok-o">{</span>

  <span class="tok-kd">public</span> <span class="tok-kt">void</span> <span class="tok-nf">printMessage</span><span class="tok-o">(</span><span class="tok-n">String</span> <span class="tok-n">var</span><span class="tok-o">)</span> <span class="tok-o">{</span>
    <span class="tok-n">System</span><span class="tok-o">.</span><span class="tok-na">out</span><span class="tok-o">.</span><span class="tok-na">println</span><span class="tok-o">(</span><span class="tok-s">&quot;hello &quot;</span> <span class="tok-o">+</span> <span class="tok-n">var</span><span class="tok-o">);</span>
  <span class="tok-o">}</span>
<span class="tok-o">}</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>Spring配置是这样的(如上所示)：</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="xml"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1
2
3
4
5
6</div></td><td class="code"><span></span><span class="tok-nt">&lt;beans&gt;</span>
  ...

  <span class="tok-nt">&lt;bean</span> <span class="tok-na">id=</span><span class="tok-s">&quot;printer&quot;</span> <span class="tok-na">class=</span><span class="tok-s">&quot;org.flowable.cmmn.examples.spring.Printer&quot;</span> <span class="tok-nt">/&gt;</span>

<span class="tok-nt">&lt;/beans&gt;</span>
</td></tr></table></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_自动部署资源">4.4. 自动部署资源</h3>
<div class="paragraph">
<p>Spring integration also has a special feature for deploying resources.  In the CMMN engine configuration, you can specify a set of resources. When the CMMN engine is created, all those resources will be scanned and deployed.  There is filtering in place that prevents duplicate deployments.  Only when the resources have actually changed will new deployments be deployed to the Flowable DB. This makes sense in a lot of use cases, where the Spring container is rebooted frequently (for example, testing).</p>
</div>
<div class="paragraph">
<p>整合spring,部署资源有一个独有的特性。CMMN引擎配置中可以定义很多资源。当CMMN引擎创建,这些资源将被扫描,发布。这个过程中将过滤重复发布的资源。当资源产生改变,将重新发布对应资源到数据库。这个在频繁重启的时候是很有意义的(比如测试时)。</p>
</div>
<div class="paragraph">
<p>Here&#8217;s an example:</p>
</div>
<div class="paragraph">
<p>请看示例：</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="xml"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1
2
3
4
5
6
7
8
9</div></td><td class="code"><span></span><span class="tok-nt">&lt;bean</span> <span class="tok-na">id=</span><span class="tok-s">&quot;cmmnEngineConfiguration&quot;</span> <span class="tok-na">class=</span><span class="tok-s">&quot;org.flowable.cmmn.spring.SpringCmmnEngineConfiguration&quot;</span><span class="tok-nt">&gt;</span>
  ...
  <span class="tok-nt">&lt;property</span> <span class="tok-na">name=</span><span class="tok-s">&quot;deploymentResources&quot;</span>
    <span class="tok-na">value=</span><span class="tok-s">&quot;classpath*:/org/flowable/cmmn/spring/test/autodeployment/autodeploy.*.cmmn&quot;</span> <span class="tok-nt">/&gt;</span>
<span class="tok-nt">&lt;/bean&gt;</span>

<span class="tok-nt">&lt;bean</span> <span class="tok-na">id=</span><span class="tok-s">&quot;cmmnEngine&quot;</span> <span class="tok-na">class=</span><span class="tok-s">&quot;org.flowable.cmmn.spring.CmmnEngineFactoryBean&quot;</span><span class="tok-nt">&gt;</span>
  <span class="tok-nt">&lt;property</span> <span class="tok-na">name=</span><span class="tok-s">&quot;cmmnEngineConfiguration&quot;</span> <span class="tok-na">ref=</span><span class="tok-s">&quot;cmmnEngineConfiguration&quot;</span> <span class="tok-nt">/&gt;</span>
<span class="tok-nt">&lt;/bean&gt;</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>以上配置默认将匹配到资源当作同一次发布(deployment)到Flowable引擎。没有更改的资源对整个发布生效。某些情况下,这可能不是所需要的。举个例子,这些发布里仅仅有一个资源文件有改动,那么发布(deployment)包含的所有资源都会被重新发布,导致实际没有变更的事例定义会产生新的版本。</p>
</div>
<div class="paragraph">
<p>针对以上的问题,可以更改 <code>SpringCmmnEngineConfiguration</code>, <code>deploymentMode</code> , 整个属性定义了如何判断资源将被部署。默认情况下该属性有一下三个选项：</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>default</code>: 默认配置,将所有资源视为一组,当作同一发布.</p>
</li>
<li>
<p><code>single-resource</code>: 单独发布,将所有分开的资源每一个当作一组发布。适用于分开发布所有流程定义,当有所改变时,只有改变的事务定义会产生新的版本号。</p>
</li>
<li>
<p><code>resource-parent-folder</code>: 目录发布,将同一文件夹下的资源文件视为一组,作为同一文件发布。适用于将不同的部署于大多数资源文件,但是依然会将共享文件夹的资源文件视为一组。下面的示例代码为使用+single-resource+ 配置 <code>deploymentMode</code>:</p>
</li>
</ul>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="xml"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1
2
3
4
5
6</div></td><td class="code"><span></span><span class="tok-nt">&lt;bean</span> <span class="tok-na">id=</span><span class="tok-s">&quot;cmmnEngineConfiguration&quot;</span>
    <span class="tok-na">class=</span><span class="tok-s">&quot;org.flowable.cmmn.spring.SpringCmmnEngineConfiguration&quot;</span><span class="tok-nt">&gt;</span>
  ...
  <span class="tok-nt">&lt;property</span> <span class="tok-na">name=</span><span class="tok-s">&quot;deploymentResources&quot;</span> <span class="tok-na">value=</span><span class="tok-s">&quot;classpath*:/flowable/*.cmmn&quot;</span> <span class="tok-nt">/&gt;</span>
  <span class="tok-nt">&lt;property</span> <span class="tok-na">name=</span><span class="tok-s">&quot;deploymentMode&quot;</span> <span class="tok-na">value=</span><span class="tok-s">&quot;single-resource&quot;</span> <span class="tok-nt">/&gt;</span>
<span class="tok-nt">&lt;/bean&gt;</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>除了以上列举的3个值来配置  <code>deploymentMode</code>, 您也可以自定义规则去检测发布,创建 <code>SpringCmmnEngineConfiguration</code>  的子类,覆盖 <code>getAutoDeploymentStrategy(String deploymentMode)</code> 方法。该方法确定 <code>deploymentMode</code> 将使用哪种策略。</p>
</div>
</div>
<div class="sect2">
<h3 id="springUnitTest">4.5. 单元测试</h3>
<div class="paragraph">
<p>整合spring 使用 <a href="#apiUnitTesting">Flowable testing facilities</a> 可以很容易去测试业务事例。
以下示例是经典的Spring-based JUnit 4或5的测试用例：</p>
</div>
<div class="listingblock">
<div class="title">JUnit 5 test</div>
<div class="content">
<pre class="pygments highlight"><code data-lang="java"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv"> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22</div></td><td class="code"><span></span><span class="tok-nd">@ExtendWith</span><span class="tok-o">(</span><span class="tok-n">FlowableCmmnSpringExtension</span><span class="tok-o">.</span><span class="tok-na">class</span><span class="tok-o">)</span>
<span class="tok-nd">@ExtendWith</span><span class="tok-o">(</span><span class="tok-n">SpringExtension</span><span class="tok-o">.</span><span class="tok-na">class</span><span class="tok-o">)</span>
<span class="tok-nd">@ContextConfiguration</span><span class="tok-o">(</span><span class="tok-n">classes</span> <span class="tok-o">=</span> <span class="tok-n">CmmnSpringJunitJupiterTest</span><span class="tok-o">.</span><span class="tok-na">TestConfiguration</span><span class="tok-o">.</span><span class="tok-na">class</span><span class="tok-o">)</span>
<span class="tok-kd">class</span> <span class="tok-nc">MyBusinessCaseTest</span> <span class="tok-o">{</span>

  <span class="tok-nd">@Autowired</span>
  <span class="tok-kd">private</span> <span class="tok-n">CmmnRepositoryService</span> <span class="tok-n">cmmnRepositoryService</span><span class="tok-o">;</span>

  <span class="tok-nd">@Autowired</span>
  <span class="tok-kd">private</span> <span class="tok-n">CmmnRuntimeService</span> <span class="tok-n">cmmnRuntimeService</span><span class="tok-o">;</span>

  <span class="tok-nd">@Test</span>
  <span class="tok-nd">@CmmnDeployment</span>
  <span class="tok-kd">public</span> <span class="tok-kt">void</span> <span class="tok-nf">simpleCaseTest</span><span class="tok-o">()</span> <span class="tok-o">{</span>
    <span class="tok-n">CaseInstance</span> <span class="tok-n">caseInstance</span> <span class="tok-o">=</span> <span class="tok-n">cmmnRuntimeService</span><span class="tok-o">.</span><span class="tok-na">createCaseInstanceBuilder</span><span class="tok-o">()</span>
    		<span class="tok-o">.</span><span class="tok-na">caseDefinitionKey</span><span class="tok-o">(</span><span class="tok-s">&quot;simpleCase&quot;</span><span class="tok-o">)</span>
            <span class="tok-o">.</span><span class="tok-na">variable</span><span class="tok-o">(</span><span class="tok-s">&quot;var1&quot;</span><span class="tok-o">,</span> <span class="tok-s">&quot;John Doe&quot;</span><span class="tok-o">)</span>
            <span class="tok-o">.</span><span class="tok-na">start</span><span class="tok-o">();</span>

	<span class="tok-n">Assertions</span><span class="tok-o">.</span><span class="tok-na">assertNotNull</span><span class="tok-o">(</span><span class="tok-n">caseInstance</span><span class="tok-o">);</span>
  <span class="tok-o">}</span>
<span class="tok-o">}</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="listingblock">
<div class="title">JUnit 4 test</div>
<div class="content">
<pre class="pygments highlight"><code data-lang="java"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv"> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17</div></td><td class="code"><span></span><span class="tok-kd">public</span> <span class="tok-kd">class</span> <span class="tok-nc">MyBusinessCaseTest</span> <span class="tok-o">{</span>

  <span class="tok-nd">@Rule</span>
  <span class="tok-kd">public</span> <span class="tok-n">FlowableCmmnRule</span> <span class="tok-n">cmmnRule</span> <span class="tok-o">=</span> <span class="tok-k">new</span> <span class="tok-n">FlowableCmmnRule</span><span class="tok-o">(</span><span class="tok-s">&quot;org/flowable/spring/test/el/SpringBeanTest-context.xml&quot;</span><span class="tok-o">);</span>

  <span class="tok-nd">@Test</span>
  <span class="tok-kd">public</span> <span class="tok-kt">void</span> <span class="tok-nf">simpleCaseTest</span><span class="tok-o">()</span> <span class="tok-o">{</span>
    <span class="tok-n">cmmnRule</span><span class="tok-o">.</span><span class="tok-na">getCmmnRepositoryService</span><span class="tok-o">().</span><span class="tok-na">createDeployment</span><span class="tok-o">().</span><span class="tok-na">addClasspathResource</span><span class="tok-o">(</span><span class="tok-s">&quot;org/flowable/spring/test/el/springExpression.cmmn&quot;</span><span class="tok-o">).</span><span class="tok-na">deploy</span><span class="tok-o">();</span>
    <span class="tok-n">CmmnRuntimeService</span> <span class="tok-n">cmmnRuntimeService</span> <span class="tok-o">=</span> <span class="tok-n">cmmnRule</span><span class="tok-o">.</span><span class="tok-na">getCmmnRuntimeService</span><span class="tok-o">();</span>
    <span class="tok-n">CaseInstance</span> <span class="tok-n">caseInstance</span> <span class="tok-o">=</span> <span class="tok-n">cmmnRuntimeService</span><span class="tok-o">.</span><span class="tok-na">createCaseInstanceBuilder</span><span class="tok-o">()</span>
    		<span class="tok-o">.</span><span class="tok-na">caseDefinitionKey</span><span class="tok-o">(</span><span class="tok-s">&quot;myCase&quot;</span><span class="tok-o">)</span>
            <span class="tok-o">.</span><span class="tok-na">variable</span><span class="tok-o">(</span><span class="tok-s">&quot;var1&quot;</span><span class="tok-o">,</span> <span class="tok-s">&quot;John Doe&quot;</span><span class="tok-o">)</span>
            <span class="tok-o">.</span><span class="tok-na">start</span><span class="tok-o">();</span>

	<span class="tok-n">Assert</span><span class="tok-o">.</span><span class="tok-na">assertNotNull</span><span class="tok-o">(</span><span class="tok-n">caseInstance</span><span class="tok-o">);</span>
  <span class="tok-o">}</span>
<span class="tok-o">}</span>
</td></tr></table></code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="chDeployment">5. 部署</h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="_外部资源">5.1. 外部资源</h3>
<div class="paragraph">
<p>案例定义位于Flowable 数据库中。当在Flowable 配置文件使用Service Tasks或Spring Beans时，这些案例定义可以引用委托类来处理。
这些类和Spring配置文件,必须可以用于所有可能执行案例定义的CMMN引擎。</p>
</div>
<div class="sect3">
<h4 id="_java_类">5.1.1. JAVA 类</h4>
<div class="paragraph">
<p>在案例定义中使用的所有自定义类（例如，服务任务中使用的PlanItemJavaDelegates）都应该在启动案例实例时，配置在引擎的类路径上。</p>
</div>
<div class="paragraph">
<p>但是，在部署案例定义期间，这些类不必配置在类路径上。也就是说在部署新的案例定义时，您的委托类不必位于类路径上，例如：当您使用演示安装程序并要添加自定义类时，应该将包含类的JAR添加到“flowable-task”或“flowable-rest” webapp lib中。别忘了还要包括自定义类（如果有的话）的依赖项。或者您也可以将依赖项配置在Tomcat安装的libraries目录中，$tomcat.home/lib（或其他Web容器的类似位置）。</p>
</div>
</div>
<div class="sect3">
<h4 id="_从案例实例中使用springbeans">5.1.2. 从案例实例中使用SpringBeans</h4>
<div class="paragraph">
<p>当在表达式或脚本使用SpringBean时，在执行案例定义时，这些bean必须对引擎可用。如果您正在构建自己的webapp，并按照 “Spring集成”章节 中的相关章节描述，在您的应用上下问文中配置CMMN引擎，那这很简单。但请记住，如果您使用"Flowable task"和"Flowable rest" webapps，那么也应该使用该上下文对其进行更新。</p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="versioningOfCaseDefinitions">5.2. 案例定义的版本管理</h3>
<div class="paragraph">
<p>CMMN没有版本控制的概念，这实际上很好，因为可执行CMMN文件可能作为开发项目的一部分存在于版本控制系统库中（如Subversion、Git或Mercurial）。但是，案例定义的版本是作为部署的一部分在引擎中创建的。在部署过程中，flowable将为CaseDefinition分配一个版本，然后将其存储在flowable数据库中</p>
</div>
<div class="paragraph">
<p>对于部署中的每个案例定义，执行以下步骤初始化属性键、版本、名称和ID：</p>
</div>
<div class="ulist">
<ul>
<li>
<p>XML文件中的案例定义+id+属性,用作案例定义+key+属性。</p>
</li>
<li>
<p>XML文件中的案例定义+名称+属性用作案例定义+名称+属性。如果未指定名称属性，则使用id属性作为名称。</p>
</li>
<li>
<p>第一次部署具有特定密钥的案例时，将分配版本1。对于具有相同键的案例定义的所有后续部署版本，版本将设置为比当前部署的最高版本高1。key属性用于区分大小写定义。</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>以下面的例子为例</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="xml"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1
2
3</div></td><td class="code"><span></span><span class="tok-nt">&lt;definitions</span> <span class="tok-na">id=</span><span class="tok-s">&quot;myDefinitions&quot;</span> <span class="tok-nt">&gt;</span>
  <span class="tok-nt">&lt;case</span> <span class="tok-na">id=</span><span class="tok-s">&quot;myCase&quot;</span> <span class="tok-na">name=</span><span class="tok-s">&quot;My important case&quot;</span> <span class="tok-nt">&gt;</span>
    ...
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>部署此案例定义时，数据库中的案例定义将看起来像这样</p>
</div>
<table class="tableblock frame-all grid-all stretch">
<colgroup>
<col style="width: 25%;">
<col style="width: 25%;">
<col style="width: 25%;">
<col style="width: 25%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">id</th>
<th class="tableblock halign-left valign-top">key</th>
<th class="tableblock halign-left valign-top">name</th>
<th class="tableblock halign-left valign-top">version</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">676</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">myCase</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">My important case</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">1</p></td>
</tr>
</tbody>
</table>
<div class="paragraph">
<p>假设我们现在部署相同案例的更新版本（例如，更改一些人工任务），但案例定义的ID保持不变。案例定义表现在将包含以下条目：</p>
</div>
<table class="tableblock frame-all grid-all stretch">
<colgroup>
<col style="width: 25%;">
<col style="width: 25%;">
<col style="width: 25%;">
<col style="width: 25%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">id</th>
<th class="tableblock halign-left valign-top">key</th>
<th class="tableblock halign-left valign-top">name</th>
<th class="tableblock halign-left valign-top">version</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">676</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">myCase</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">My important case</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">1</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">870</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">myCase</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">My important case</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">2</p></td>
</tr>
</tbody>
</table>
<div class="paragraph">
<p>当调用RuntimeService.CreateCaseInstanceBuilder（）.CaseDefinitionKey（“MyCase”）.Start（）时，它现在将在版本2中使用案例定义，因为这是案例定义的最新版本。</p>
</div>
<div class="paragraph">
<p>如果我们像下面定义的那样创建第二个案例并将其部署到Flowable，那么第三行将添加到表中。</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="xml"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1
2
3</div></td><td class="code"><span></span><span class="tok-nt">&lt;definitions</span> <span class="tok-na">id=</span><span class="tok-s">&quot;myNewDefinitions&quot;</span> <span class="tok-nt">&gt;</span>
  <span class="tok-nt">&lt;case</span> <span class="tok-na">id=</span><span class="tok-s">&quot;myNewCase&quot;</span> <span class="tok-na">name=</span><span class="tok-s">&quot;My important case&quot;</span> <span class="tok-nt">&gt;</span>
    ...
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>这个表看起来像这样：</p>
</div>
<table class="tableblock frame-all grid-all stretch">
<colgroup>
<col style="width: 25%;">
<col style="width: 25%;">
<col style="width: 25%;">
<col style="width: 25%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">id</th>
<th class="tableblock halign-left valign-top">key</th>
<th class="tableblock halign-left valign-top">name</th>
<th class="tableblock halign-left valign-top">version</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">676</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">myCase</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">My important case</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">1</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">870</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">myCase</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">My important case</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">2</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">1033</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">myNewCase</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">My important case</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">1</p></td>
</tr>
</tbody>
</table>
<div class="paragraph">
<p>请注意：新案例的关键与第一个案例有何不同？尽管名称是相同的（我们可能也该更改它），但在区分大小写时，Flowable只考虑ID属性。因此，新案例会与版本1一起部署。</p>
</div>
</div>
<div class="sect2">
<h3 id="deploymentCategory">5.3. 分类</h3>
<div class="paragraph">
<p>部署和案例定义都有用户定义的类别。案例定义类别是用CMMN XML中的“targetNamespace”属性的值初始化的：&lt;definitions…targetNamespace=“您定义的类别”…</p>
</div>
<div class="paragraph">
<p>部署类别也可以在API中指定，如下所示：</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="java"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1
2
3
4
5</div></td><td class="code"><span></span><span class="tok-n">repositoryService</span>
    <span class="tok-o">.</span><span class="tok-na">createDeployment</span><span class="tok-o">()</span>
    <span class="tok-o">.</span><span class="tok-na">category</span><span class="tok-o">(</span><span class="tok-s">&quot;yourCategory&quot;</span><span class="tok-o">)</span>
    <span class="tok-o">...</span>
    <span class="tok-o">.</span><span class="tok-na">deploy</span><span class="tok-o">();</span>
</td></tr></table></code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_cmmn_1_1">6. CMMN 1.1</h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="_什么是_cmmn">6.1. 什么是 CMMN?</h3>
<div class="paragraph">
<p>案例管理模型和标记法是由<a href="http://www.omg.org/spec/CMMN/">对象管理组织(Object Management Group,缩写为OMG)</a>为了表示案例模型而建立的标准规范.</p>
</div>
<div class="paragraph">
<p>Flowable 包含:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>一个基于CMMN1.1的模型设计器用来创建案例模型</p>
</li>
<li>
<p>一个JAVA引擎用来导入并运行CMMN1.1案例模型</p>
</li>
<li>
<p>一个动态前端页面用来执行案例模型,并允许用户查看并完成相关任务和对应表单</p>
</li>
</ul>
</div>
</div>
<div class="sect2">
<h3 id="_基础概念和术语">6.2. 基础概念和术语</h3>
<div class="paragraph">
<p>以下内容展示了一个简单的CMMN1.1图:</p>
</div>
<div class="imageblock" style="text-align: center">
<div class="content">
<img src="images/cmmn-basic-concepts.png" alt="cmmn basic concepts">
</div>
</div>
<div class="paragraph">
<p>一个 <strong>案例模型</strong> 通常被看作一个包含所有的案例元素的 <em>文件夹</em>. 每个案例模型包含一个 <strong>计划模型</strong> ,其他所有的元素放在该计划模型内部.</p>
</div>
<div class="paragraph">
<p>一个计划模型中所有元素被称为 <strong>计划项</strong>. 每个计划项都一个类型及运行时相关配置项的定义.例如,上图中,有三个人工任务和一个里程碑.其他的计划项分别有 <em>流程任务、案例任务和阶段</em>.</p>
</div>
<div class="paragraph">
<p>当把一个案例模型在Flowable CMMN引擎上部署完成后, 很可能就直接启动一个基于此模型的实例. 其中的定义的计划项同样也会产成运行时的实例并且暴露出来,通过使用Flowable API可以查询得到. <strong>计划项实例</strong> 拥有基于CMMN 1.1规范定义的生命周期状态, 并且是整个引擎工作的核心. 详细内容请查阅 CMMN1.1规范的8.4.2部分.</p>
</div>
<div class="paragraph">
<p>计划项可以拥有 <em>哨兵</em>: 当使用哨兵守卫它的激活, 这个计划项相当于拥有入口凭证.这些凭证明确了触发哨兵需要满足的条件.例如,在上图中,里程碑在一个案例实例启动后为可用状态,但是当人工任务A和人工任务B都完成后,该里程碑状态由可用状态变成活跃状态.注意守卫可以在它的条件中使用不可见的复杂表达式,以便完成更多复杂的功能.同样,也可以拥有多个守卫,只需满足一个哨兵就可以出发状态的转变.</p>
</div>
<div class="paragraph">
<p>计划项和计划模型也可以拥有退出凭证的哨兵,其明确了从对应计划项退出的条件.在上图中,当人工任务C完成后,整个计划模型(包括此刻所有活跃状态的子元素)将会退出.</p>
</div>
<div class="paragraph">
<p>CMMN 1.1在XSD中定义作为规范一部分的XML标准格式. 已供参阅, 下面展示的XML代表了上述例子的案例图.</p>
</div>
<div class="paragraph">
<p>一些结论:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>XML中的四个计划项通过使用 <em>definitionRef</em> 属性关联它们自身的定义. 实际的声明是在底部的 <em>casePlanModel</em> 元素</p>
</li>
<li>
<p>拥有进入或退出凭证的计划项关联一个 <em>sentry</em> 哨兵(而不是反过来)</p>
</li>
<li>
<p>XML也包含可见案例图的信息(x 和 y坐标, 宽度和高度等等),在以下展示中已省略. 当使用其他 CMMN 1.1 模型工具用来保存正确的可视化案例图时,这些信息是重要的.</p>
</li>
</ul>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="xml"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv"> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52</div></td><td class="code"><span></span><span class="tok-cp">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span>
<span class="tok-nt">&lt;definitions</span> <span class="tok-na">xmlns=</span><span class="tok-s">&quot;http://www.omg.org/spec/CMMN/20151109/MODEL&quot;</span>
             <span class="tok-na">xmlns:xsi=</span><span class="tok-s">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span>
             <span class="tok-na">xmlns:flowable=</span><span class="tok-s">&quot;http://flowable.org/cmmn&quot;</span>
             <span class="tok-na">xmlns:cmmndi=</span><span class="tok-s">&quot;http://www.omg.org/spec/CMMN/20151109/CMMNDI&quot;</span>
             <span class="tok-na">xmlns:dc=</span><span class="tok-s">&quot;http://www.omg.org/spec/CMMN/20151109/DC&quot;</span>
             <span class="tok-na">xmlns:di=</span><span class="tok-s">&quot;http://www.omg.org/spec/CMMN/20151109/DI&quot;</span>
             <span class="tok-na">targetNamespace=</span><span class="tok-s">&quot;http://www.flowable.org/casedef&quot;</span><span class="tok-nt">&gt;</span>
  <span class="tok-nt">&lt;case</span> <span class="tok-na">id=</span><span class="tok-s">&quot;simpleExample&quot;</span> <span class="tok-na">name=</span><span class="tok-s">&quot;Simple Example&quot;</span><span class="tok-nt">&gt;</span>
    <span class="tok-nt">&lt;casePlanModel</span> <span class="tok-na">id=</span><span class="tok-s">&quot;casePlanModel&quot;</span> <span class="tok-na">name=</span><span class="tok-s">&quot;My Case&quot;</span><span class="tok-nt">&gt;</span>
      <span class="tok-nt">&lt;planItem</span> <span class="tok-na">id=</span><span class="tok-s">&quot;planItem1&quot;</span> <span class="tok-na">name=</span><span class="tok-s">&quot;Human task A&quot;</span>
            <span class="tok-na">definitionRef=</span><span class="tok-s">&quot;sid-88199E7C-7655-439C-810B-8849FC52D3EB&quot;</span><span class="tok-nt">&gt;&lt;/planItem&gt;</span>
      <span class="tok-nt">&lt;planItem</span> <span class="tok-na">id=</span><span class="tok-s">&quot;planItem2&quot;</span> <span class="tok-na">name=</span><span class="tok-s">&quot;Milestone One&quot;</span>
            <span class="tok-na">definitionRef=</span><span class="tok-s">&quot;sid-8BF8A774-A8A7-4F1A-95CF-1E0D61EE5A47&quot;</span><span class="tok-nt">&gt;</span>
        <span class="tok-nt">&lt;entryCriterion</span> <span class="tok-na">id=</span><span class="tok-s">&quot;sid-62CC4A6D-B29B-4129-93EA-460253C45CDF&quot;</span>
            <span class="tok-na">sentryRef=</span><span class="tok-s">&quot;sentry1&quot;</span><span class="tok-nt">&gt;&lt;/entryCriterion&gt;</span>
      <span class="tok-nt">&lt;/planItem&gt;</span>
      <span class="tok-nt">&lt;planItem</span> <span class="tok-na">id=</span><span class="tok-s">&quot;planItem3&quot;</span> <span class="tok-na">name=</span><span class="tok-s">&quot;Human task B&quot;</span>
            <span class="tok-na">definitionRef=</span><span class="tok-s">&quot;sid-A1FB8733-0DBC-4B38-9830-CBC4D0C4B802&quot;</span><span class="tok-nt">&gt;&lt;/planItem&gt;</span>
      <span class="tok-nt">&lt;planItem</span> <span class="tok-na">id=</span><span class="tok-s">&quot;planItem4&quot;</span> <span class="tok-na">name=</span><span class="tok-s">&quot;Human task C&quot;</span>
            <span class="tok-na">definitionRef=</span><span class="tok-s">&quot;sid-D3970AFC-7391-4BA7-95BA-51C64D2F41E9&quot;</span><span class="tok-nt">&gt;&lt;/planItem&gt;</span>
      <span class="tok-nt">&lt;sentry</span> <span class="tok-na">id=</span><span class="tok-s">&quot;sentry1&quot;</span><span class="tok-nt">&gt;</span>
        <span class="tok-nt">&lt;planItemOnPart</span> <span class="tok-na">id=</span><span class="tok-s">&quot;sentryOnPart1&quot;</span> <span class="tok-na">sourceRef=</span><span class="tok-s">&quot;planItem1&quot;</span><span class="tok-nt">&gt;</span>
          <span class="tok-nt">&lt;standardEvent&gt;</span>complete<span class="tok-nt">&lt;/standardEvent&gt;</span>
        <span class="tok-nt">&lt;/planItemOnPart&gt;</span>
        <span class="tok-nt">&lt;planItemOnPart</span> <span class="tok-na">id=</span><span class="tok-s">&quot;sentryOnPart2&quot;</span> <span class="tok-na">sourceRef=</span><span class="tok-s">&quot;planItem3&quot;</span><span class="tok-nt">&gt;</span>
          <span class="tok-nt">&lt;standardEvent&gt;</span>complete<span class="tok-nt">&lt;/standardEvent&gt;</span>
        <span class="tok-nt">&lt;/planItemOnPart&gt;</span>
      <span class="tok-nt">&lt;/sentry&gt;</span>
      <span class="tok-nt">&lt;sentry</span> <span class="tok-na">id=</span><span class="tok-s">&quot;sentry2&quot;</span><span class="tok-nt">&gt;</span>
        <span class="tok-nt">&lt;planItemOnPart</span> <span class="tok-na">id=</span><span class="tok-s">&quot;sentryOnPart3&quot;</span> <span class="tok-na">sourceRef=</span><span class="tok-s">&quot;planItem4&quot;</span><span class="tok-nt">&gt;</span>
          <span class="tok-nt">&lt;standardEvent&gt;</span>complete<span class="tok-nt">&lt;/standardEvent&gt;</span>
        <span class="tok-nt">&lt;/planItemOnPart&gt;</span>
      <span class="tok-nt">&lt;/sentry&gt;</span>
      <span class="tok-nt">&lt;humanTask</span> <span class="tok-na">id=</span><span class="tok-s">&quot;sid-88199E7C-7655-439C-810B-8849FC52D3EB&quot;</span>
        <span class="tok-na">name=</span><span class="tok-s">&quot;Human task A&quot;</span><span class="tok-nt">&gt;&lt;/humanTask&gt;</span>
      <span class="tok-nt">&lt;milestone</span> <span class="tok-na">id=</span><span class="tok-s">&quot;sid-8BF8A774-A8A7-4F1A-95CF-1E0D61EE5A47&quot;</span>
        <span class="tok-na">name=</span><span class="tok-s">&quot;Milestone One&quot;</span><span class="tok-nt">&gt;&lt;/milestone&gt;</span>
      <span class="tok-nt">&lt;humanTask</span> <span class="tok-na">id=</span><span class="tok-s">&quot;sid-A1FB8733-0DBC-4B38-9830-CBC4D0C4B802&quot;</span>
        <span class="tok-na">name=</span><span class="tok-s">&quot;Human task B&quot;</span><span class="tok-nt">&gt;&lt;/humanTask&gt;</span>
      <span class="tok-nt">&lt;humanTask</span> <span class="tok-na">id=</span><span class="tok-s">&quot;sid-D3970AFC-7391-4BA7-95BA-51C64D2F41E9&quot;</span>
        <span class="tok-na">name=</span><span class="tok-s">&quot;Human task C&quot;</span><span class="tok-nt">&gt;&lt;/humanTask&gt;</span>
      <span class="tok-nt">&lt;exitCriterion</span> <span class="tok-na">id=</span><span class="tok-s">&quot;sid-422626DB-9B40-49D8-955E-641AB96A5BFA&quot;</span>
        <span class="tok-na">sentryRef=</span><span class="tok-s">&quot;sentry2&quot;</span><span class="tok-nt">&gt;&lt;/exitCriterion&gt;</span>
    <span class="tok-nt">&lt;/casePlanModel&gt;</span>
  <span class="tok-nt">&lt;/case&gt;</span>
  <span class="tok-nt">&lt;cmmndi:CMMNDI&gt;</span>
    <span class="tok-nt">&lt;cmmndi:CMMNDiagram</span> <span class="tok-na">id=</span><span class="tok-s">&quot;CMMNDiagram_simpleExample&quot;</span><span class="tok-nt">&gt;</span>
        ...
    <span class="tok-nt">&lt;/cmmndi:CMMNDiagram&gt;</span>
  <span class="tok-nt">&lt;/cmmndi:CMMNDI&gt;</span>
<span class="tok-nt">&lt;/definitions&gt;</span>
</td></tr></table></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_程序示例">6.3. 程序示例</h3>
<div class="paragraph">
<p>在这个小节, 我们将会用代码构建并执行一个简单的案例模型, 通过在命令行中调用Flowable CMMN engine的Java API.</p>
</div>
<div class="paragraph">
<p>在这个案例模型中, 我们将构建一个简化的有两个阶段的 <em>employee onboarding</em> 案例: 两个时期分别在的员工入职前后.第一个阶段,HR部门中的某个人将会完成相关任务;第二个阶段,该员工完成它们.同时,在任何时间,该试用员工可以拒绝这份工作并且停止这个实例.</p>
</div>
<div class="paragraph">
<p>注意, 只有 stages and human tasks 被用到. 在真实案例模型中, 很可能会有其他类型的计划项,比如里程碑, 嵌套stage, 自动化任务等等.</p>
</div>
<div class="imageblock" style="text-align: center">
<div class="content">
<img src="images/cmmn.programmatic.example.png" alt="cmmn.programmatic.example">
</div>
</div>
<div class="paragraph">
<p>这个案例模型的XML如下所示:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="xml"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv"> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85</div></td><td class="code"><span></span><span class="tok-cp">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span>
<span class="tok-nt">&lt;definitions</span> <span class="tok-na">xmlns=</span><span class="tok-s">&quot;http://www.omg.org/spec/CMMN/20151109/MODEL&quot;</span>
             <span class="tok-na">xmlns:xsi=</span><span class="tok-s">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span>
             <span class="tok-na">xmlns:flowable=</span><span class="tok-s">&quot;http://flowable.org/cmmn&quot;</span>
             <span class="tok-na">xmlns:cmmndi=</span><span class="tok-s">&quot;http://www.omg.org/spec/CMMN/20151109/CMMNDI&quot;</span>
             <span class="tok-na">xmlns:dc=</span><span class="tok-s">&quot;http://www.omg.org/spec/CMMN/20151109/DC&quot;</span>
             <span class="tok-na">xmlns:di=</span><span class="tok-s">&quot;http://www.omg.org/spec/CMMN/20151109/DI&quot;</span>
             <span class="tok-na">targetNamespace=</span><span class="tok-s">&quot;http://www.flowable.org/casedef&quot;</span><span class="tok-nt">&gt;</span>
  <span class="tok-nt">&lt;case</span> <span class="tok-na">id=</span><span class="tok-s">&quot;employeeOnboarding&quot;</span> <span class="tok-na">name=</span><span class="tok-s">&quot;Simple Example&quot;</span><span class="tok-nt">&gt;</span>
    <span class="tok-nt">&lt;casePlanModel</span> <span class="tok-na">id=</span><span class="tok-s">&quot;casePlanModel&quot;</span> <span class="tok-na">name=</span><span class="tok-s">&quot;My Case&quot;</span><span class="tok-nt">&gt;</span>
      <span class="tok-nt">&lt;planItem</span> <span class="tok-na">id=</span><span class="tok-s">&quot;planItem5&quot;</span> <span class="tok-na">name=</span><span class="tok-s">&quot;Prior to starting&quot;</span>
            <span class="tok-na">definitionRef=</span><span class="tok-s">&quot;sid-025D29E8-BA9B-403D-A684-8C5B52185642&quot;</span><span class="tok-nt">&gt;&lt;/planItem&gt;</span>
      <span class="tok-nt">&lt;planItem</span> <span class="tok-na">id=</span><span class="tok-s">&quot;planItem8&quot;</span> <span class="tok-na">name=</span><span class="tok-s">&quot;After starting&quot;</span>
            <span class="tok-na">definitionRef=</span><span class="tok-s">&quot;sid-8459EF32-4F4C-4E9B-A6E9-87FDC2299044&quot;</span><span class="tok-nt">&gt;</span>
        <span class="tok-nt">&lt;entryCriterion</span> <span class="tok-na">id=</span><span class="tok-s">&quot;sid-50B5F12D-FE75-4D05-9148-86574EE6C073&quot;</span>
            <span class="tok-na">sentryRef=</span><span class="tok-s">&quot;sentry2&quot;</span><span class="tok-nt">&gt;&lt;/entryCriterion&gt;</span>
      <span class="tok-nt">&lt;/planItem&gt;</span>
      <span class="tok-nt">&lt;planItem</span> <span class="tok-na">id=</span><span class="tok-s">&quot;planItem9&quot;</span> <span class="tok-na">name=</span><span class="tok-s">&quot;Reject job&quot;</span>
            <span class="tok-na">definitionRef=</span><span class="tok-s">&quot;sid-134E885A-3D58-417E-81E2-66A3E12334F9&quot;</span><span class="tok-nt">&gt;&lt;/planItem&gt;</span>
      <span class="tok-nt">&lt;sentry</span> <span class="tok-na">id=</span><span class="tok-s">&quot;sentry2&quot;</span><span class="tok-nt">&gt;</span>
        <span class="tok-nt">&lt;planItemOnPart</span> <span class="tok-na">id=</span><span class="tok-s">&quot;sentryOnPart4&quot;</span> <span class="tok-na">sourceRef=</span><span class="tok-s">&quot;planItem5&quot;</span><span class="tok-nt">&gt;</span>
          <span class="tok-nt">&lt;standardEvent&gt;</span>complete<span class="tok-nt">&lt;/standardEvent&gt;</span>
        <span class="tok-nt">&lt;/planItemOnPart&gt;</span>
      <span class="tok-nt">&lt;/sentry&gt;</span>
      <span class="tok-nt">&lt;sentry</span> <span class="tok-na">id=</span><span class="tok-s">&quot;sentry3&quot;</span><span class="tok-nt">&gt;</span>
        <span class="tok-nt">&lt;planItemOnPart</span> <span class="tok-na">id=</span><span class="tok-s">&quot;sentryOnPart5&quot;</span> <span class="tok-na">sourceRef=</span><span class="tok-s">&quot;planItem9&quot;</span><span class="tok-nt">&gt;</span>
          <span class="tok-nt">&lt;standardEvent&gt;</span>complete<span class="tok-nt">&lt;/standardEvent&gt;</span>
        <span class="tok-nt">&lt;/planItemOnPart&gt;</span>
      <span class="tok-nt">&lt;/sentry&gt;</span>
      <span class="tok-nt">&lt;stage</span> <span class="tok-na">id=</span><span class="tok-s">&quot;sid-025D29E8-BA9B-403D-A684-8C5B52185642&quot;</span> <span class="tok-na">name=</span><span class="tok-s">&quot;Prior to starting&quot;</span><span class="tok-nt">&gt;</span>
        <span class="tok-nt">&lt;planItem</span> <span class="tok-na">id=</span><span class="tok-s">&quot;planItem1&quot;</span> <span class="tok-na">name=</span><span class="tok-s">&quot;Create email address&quot;</span>
                <span class="tok-na">definitionRef=</span><span class="tok-s">&quot;sid-EA434DDD-E1BE-4AC1-8520-B19ACE8782D2&quot;</span><span class="tok-nt">&gt;&lt;/planItem&gt;</span>
        <span class="tok-nt">&lt;planItem</span> <span class="tok-na">id=</span><span class="tok-s">&quot;planItem2&quot;</span> <span class="tok-na">name=</span><span class="tok-s">&quot;Allocate office&quot;</span>
                <span class="tok-na">definitionRef=</span><span class="tok-s">&quot;sid-505BA223-131A-4EF0-ABAD-485AEB0F2C96&quot;</span><span class="tok-nt">&gt;&lt;/planItem&gt;</span>
        <span class="tok-nt">&lt;planItem</span> <span class="tok-na">id=</span><span class="tok-s">&quot;planItem3&quot;</span> <span class="tok-na">name=</span><span class="tok-s">&quot;Send joining letter to candidate&quot;</span>
                <span class="tok-na">definitionRef=</span><span class="tok-s">&quot;sid-D28DBAD5-0F5F-45F4-8553-3381199AC45F&quot;</span><span class="tok-nt">&gt;</span>
          <span class="tok-nt">&lt;entryCriterion</span> <span class="tok-na">id=</span><span class="tok-s">&quot;sid-4D88C79D-8E31-4246-9541-A4F6A5720AC8&quot;</span>
            <span class="tok-na">sentryRef=</span><span class="tok-s">&quot;sentry1&quot;</span><span class="tok-nt">&gt;&lt;/entryCriterion&gt;</span>
        <span class="tok-nt">&lt;/planItem&gt;</span>
        <span class="tok-nt">&lt;planItem</span> <span class="tok-na">id=</span><span class="tok-s">&quot;planItem4&quot;</span> <span class="tok-na">name=</span><span class="tok-s">&quot;Agree start date&quot;</span>
                <span class="tok-na">definitionRef=</span><span class="tok-s">&quot;sid-97A72C46-C0AD-477F-86DD-85EF643BB97D&quot;</span><span class="tok-nt">&gt;&lt;/planItem&gt;</span>
        <span class="tok-nt">&lt;sentry</span> <span class="tok-na">id=</span><span class="tok-s">&quot;sentry1&quot;</span><span class="tok-nt">&gt;</span>
          <span class="tok-nt">&lt;planItemOnPart</span> <span class="tok-na">id=</span><span class="tok-s">&quot;sentryOnPart1&quot;</span> <span class="tok-na">sourceRef=</span><span class="tok-s">&quot;planItem1&quot;</span><span class="tok-nt">&gt;</span>
            <span class="tok-nt">&lt;standardEvent&gt;</span>complete<span class="tok-nt">&lt;/standardEvent&gt;</span>
          <span class="tok-nt">&lt;/planItemOnPart&gt;</span>
          <span class="tok-nt">&lt;planItemOnPart</span> <span class="tok-na">id=</span><span class="tok-s">&quot;sentryOnPart2&quot;</span> <span class="tok-na">sourceRef=</span><span class="tok-s">&quot;planItem2&quot;</span><span class="tok-nt">&gt;</span>
            <span class="tok-nt">&lt;standardEvent&gt;</span>complete<span class="tok-nt">&lt;/standardEvent&gt;</span>
          <span class="tok-nt">&lt;/planItemOnPart&gt;</span>
          <span class="tok-nt">&lt;planItemOnPart</span> <span class="tok-na">id=</span><span class="tok-s">&quot;sentryOnPart3&quot;</span> <span class="tok-na">sourceRef=</span><span class="tok-s">&quot;planItem4&quot;</span><span class="tok-nt">&gt;</span>
            <span class="tok-nt">&lt;standardEvent&gt;</span>complete<span class="tok-nt">&lt;/standardEvent&gt;</span>
          <span class="tok-nt">&lt;/planItemOnPart&gt;</span>
        <span class="tok-nt">&lt;/sentry&gt;</span>
        <span class="tok-nt">&lt;humanTask</span> <span class="tok-na">id=</span><span class="tok-s">&quot;sid-EA434DDD-E1BE-4AC1-8520-B19ACE8782D2&quot;</span>
            <span class="tok-na">name=</span><span class="tok-s">&quot;Create email address&quot;</span>
            <span class="tok-na">flowable:candidateGroups=</span><span class="tok-s">&quot;hr&quot;</span><span class="tok-nt">&gt;&lt;/humanTask&gt;</span>
        <span class="tok-nt">&lt;humanTask</span> <span class="tok-na">id=</span><span class="tok-s">&quot;sid-505BA223-131A-4EF0-ABAD-485AEB0F2C96&quot;</span>
            <span class="tok-na">name=</span><span class="tok-s">&quot;Allocate office&quot;</span>
            <span class="tok-na">flowable:candidateGroups=</span><span class="tok-s">&quot;hr&quot;</span><span class="tok-nt">&gt;&lt;/humanTask&gt;</span>
        <span class="tok-nt">&lt;humanTask</span> <span class="tok-na">id=</span><span class="tok-s">&quot;sid-D28DBAD5-0F5F-45F4-8553-3381199AC45F&quot;</span>
            <span class="tok-na">name=</span><span class="tok-s">&quot;Send joining letter to candidate&quot;</span>
            <span class="tok-na">flowable:candidateGroups=</span><span class="tok-s">&quot;hr&quot;</span><span class="tok-nt">&gt;&lt;/humanTask&gt;</span>
        <span class="tok-nt">&lt;humanTask</span> <span class="tok-na">id=</span><span class="tok-s">&quot;sid-97A72C46-C0AD-477F-86DD-85EF643BB97D&quot;</span>
            <span class="tok-na">name=</span><span class="tok-s">&quot;Agree start date&quot;</span>
            <span class="tok-na">flowable:candidateGroups=</span><span class="tok-s">&quot;hr&quot;</span><span class="tok-nt">&gt;&lt;/humanTask&gt;</span>
      <span class="tok-nt">&lt;/stage&gt;</span>
      <span class="tok-nt">&lt;stage</span> <span class="tok-na">id=</span><span class="tok-s">&quot;sid-8459EF32-4F4C-4E9B-A6E9-87FDC2299044&quot;</span>
        <span class="tok-na">name=</span><span class="tok-s">&quot;After starting&quot;</span><span class="tok-nt">&gt;</span>
        <span class="tok-nt">&lt;planItem</span> <span class="tok-na">id=</span><span class="tok-s">&quot;planItem6&quot;</span> <span class="tok-na">name=</span><span class="tok-s">&quot;New starter training&quot;</span>
                <span class="tok-na">definitionRef=</span><span class="tok-s">&quot;sid-DF7B9582-11A6-40B4-B7E5-EC7AC6029387&quot;</span><span class="tok-nt">&gt;&lt;/planItem&gt;</span>
        <span class="tok-nt">&lt;planItem</span> <span class="tok-na">id=</span><span class="tok-s">&quot;planItem7&quot;</span> <span class="tok-na">name=</span><span class="tok-s">&quot;Fill in paperwork&quot;</span>
                <span class="tok-na">definitionRef=</span><span class="tok-s">&quot;sid-7BF2B421-7FA0-479D-A8BD-C22EBD09F599&quot;</span><span class="tok-nt">&gt;&lt;/planItem&gt;</span>
        <span class="tok-nt">&lt;humanTask</span> <span class="tok-na">id=</span><span class="tok-s">&quot;sid-DF7B9582-11A6-40B4-B7E5-EC7AC6029387&quot;</span>
            <span class="tok-na">name=</span><span class="tok-s">&quot;New starter training&quot;</span>
            <span class="tok-na">flowable:assignee=</span><span class="tok-s">&quot;${potentialEmployee}&quot;</span><span class="tok-nt">&gt;&lt;/humanTask&gt;</span>
        <span class="tok-nt">&lt;humanTask</span> <span class="tok-na">id=</span><span class="tok-s">&quot;sid-7BF2B421-7FA0-479D-A8BD-C22EBD09F599&quot;</span>
            <span class="tok-na">name=</span><span class="tok-s">&quot;Fill in paperwork&quot;</span>
            <span class="tok-na">flowable:assignee=</span><span class="tok-s">&quot;${potentialEmployee}&quot;</span><span class="tok-nt">&gt;&lt;/humanTask&gt;</span>
      <span class="tok-nt">&lt;/stage&gt;</span>
      <span class="tok-nt">&lt;humanTask</span> <span class="tok-na">id=</span><span class="tok-s">&quot;sid-134E885A-3D58-417E-81E2-66A3E12334F9&quot;</span> <span class="tok-na">name=</span><span class="tok-s">&quot;Reject job&quot;</span>
        <span class="tok-na">flowable:assignee=</span><span class="tok-s">&quot;${potentialEmployee}&quot;</span><span class="tok-nt">&gt;&lt;/humanTask&gt;</span>
      <span class="tok-nt">&lt;exitCriterion</span> <span class="tok-na">id=</span><span class="tok-s">&quot;sid-18277F30-E146-4B3E-B3C9-3F1E187EC7A8&quot;</span>
        <span class="tok-na">sentryRef=</span><span class="tok-s">&quot;sentry3&quot;</span><span class="tok-nt">&gt;&lt;/exitCriterion&gt;</span>
    <span class="tok-nt">&lt;/casePlanModel&gt;</span>
  <span class="tok-nt">&lt;/case&gt;</span>
<span class="tok-nt">&lt;/definitions&gt;</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>首先, 创建一个新的工程, 添加 <em>flowable-cmmn-engine</em> 依赖(这里展示了Maven), H2 数据库依赖也要添加, 后续将会使用 H2 作为内嵌的数据库.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="xml"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv"> 1
 2
 3
 4
 5
 6
 7
 8
 9
10</div></td><td class="code"><span></span><span class="tok-nt">&lt;dependency&gt;</span>
    <span class="tok-nt">&lt;groupId&gt;</span>org.flowable<span class="tok-nt">&lt;/groupId&gt;</span>
    <span class="tok-nt">&lt;artifactId&gt;</span>flowable-cmmn-engine<span class="tok-nt">&lt;/artifactId&gt;</span>
    <span class="tok-nt">&lt;version&gt;</span>${flowable.version}<span class="tok-nt">&lt;/version&gt;</span>
<span class="tok-nt">&lt;/dependency&gt;</span>
<span class="tok-nt">&lt;dependency&gt;</span>
    <span class="tok-nt">&lt;groupId&gt;</span>com.h2database<span class="tok-nt">&lt;/groupId&gt;</span>
    <span class="tok-nt">&lt;artifactId&gt;</span>h2<span class="tok-nt">&lt;/artifactId&gt;</span>
    <span class="tok-nt">&lt;version&gt;</span>${h2.version}<span class="tok-nt">&lt;/version&gt;</span>
<span class="tok-nt">&lt;/dependency&gt;</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>The Flowable CMMN API 包括了其他的 Flowable APIs 和相关概念. 同样,熟悉 BPMN 或者 DMN APIs的人很容易上手.和其他引擎一样, 第一行代码是创建一个 Cmmn引擎. 这里,默认的配置将会使用 H2作为内存数据库:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="java"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1
2
3
4
5
6</div></td><td class="code"><span></span><span class="tok-kd">public</span> <span class="tok-kd">class</span> <span class="tok-nc">Main</span> <span class="tok-o">{</span>
  <span class="tok-kd">public</span> <span class="tok-kd">static</span> <span class="tok-kt">void</span> <span class="tok-nf">main</span><span class="tok-o">(</span><span class="tok-n">String</span><span class="tok-o">[]</span> <span class="tok-n">args</span><span class="tok-o">)</span> <span class="tok-o">{</span>
    <span class="tok-n">CmmnEngine</span> <span class="tok-n">cmmnEngine</span>
        <span class="tok-o">=</span> <span class="tok-k">new</span> <span class="tok-n">StandaloneInMemCmmnEngineConfiguration</span><span class="tok-o">().</span><span class="tok-na">buildCmmnEngine</span><span class="tok-o">();</span>
  <span class="tok-o">}</span>
<span class="tok-o">}</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>注意_CmmnEngineConfiguration_ 提供了很多配置项来调整 CMMN引擎的设置.</p>
</div>
<div class="paragraph">
<p>把上述 XML 放入一个文件中, 例如 <em>my-case.cmmn</em> (or .cmmn.xml). 对于Maven工程, 应该放在 <em>src/main/resources</em> 文件夹中.</p>
</div>
<div class="paragraph">
<p>为了使引擎感知当前案例模型, 它首先需要被部署 <em>deployed</em>. 这个工作由 the <em>CmmnRepositoryService</em> 完成:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="java"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1
2
3
4</div></td><td class="code"><span></span><span class="tok-n">CmmnRepositoryService</span> <span class="tok-n">cmmnRepositoryService</span> <span class="tok-o">=</span> <span class="tok-n">cmmnEngine</span><span class="tok-o">.</span><span class="tok-na">getCmmnRepositoryService</span><span class="tok-o">();</span>
<span class="tok-n">CmmnDeployment</span> <span class="tok-n">cmmnDeployment</span> <span class="tok-o">=</span> <span class="tok-n">cmmnRepositoryService</span><span class="tok-o">.</span><span class="tok-na">createDeployment</span><span class="tok-o">()</span>
    <span class="tok-o">.</span><span class="tok-na">addClasspathResource</span><span class="tok-o">(</span><span class="tok-s">&quot;my-case.cmmn&quot;</span><span class="tok-o">)</span>
    <span class="tok-o">.</span><span class="tok-na">deploy</span><span class="tok-o">();</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>当部署XML时, 会返回一个 <strong>CmmnDeployment</strong>. 一个 deployment 包含多个案例模型和目标. 一个明确的案例模型定义被存储成一个案例定义 <strong>CaseDefinition</strong>. 这可以通过执行 <em>CaseDefinitionQuery</em> 案例定义查询 来验证:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="java"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1
2</div></td><td class="code"><span></span><span class="tok-n">List</span><span class="tok-o">&lt;</span><span class="tok-n">CaseDefinition</span><span class="tok-o">&gt;</span> <span class="tok-n">caseDefinitions</span> <span class="tok-o">=</span> <span class="tok-n">cmmnRepositoryService</span><span class="tok-o">.</span><span class="tok-na">createCaseDefinitionQuery</span><span class="tok-o">().</span><span class="tok-na">list</span><span class="tok-o">();</span>
<span class="tok-n">System</span><span class="tok-o">.</span><span class="tok-na">out</span><span class="tok-o">.</span><span class="tok-na">println</span><span class="tok-o">(</span><span class="tok-s">&quot;Found &quot;</span> <span class="tok-o">+</span> <span class="tok-n">caseDefinitions</span><span class="tok-o">.</span><span class="tok-na">size</span><span class="tok-o">()</span> <span class="tok-o">+</span> <span class="tok-s">&quot; case definitions&quot;</span><span class="tok-o">);</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>当在引擎中拥有一个案例定义后, 现在可以启动一个对应 <strong>CaseInstance</strong> 案例实例. 要么使用查询后的结果并传递给下面的代码片段中, 要么直接使用 the case definition的key(如下所示).</p>
</div>
<div class="paragraph">
<p>注意当启动 <strong>CaseInstance</strong>, 我们也传递了 <em>potentialEmployee</em> 作为一个标记, 这个变量之后会被用作人工任务的指派(see the <em>assignee="${potentialEmployee}"</em> attribute on <em>human tasks</em>)</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="java"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1
2
3
4
5</div></td><td class="code"><span></span><span class="tok-n">CmmnRuntimeService</span> <span class="tok-n">cmmnRuntimeService</span> <span class="tok-o">=</span> <span class="tok-n">cmmnEngine</span><span class="tok-o">.</span><span class="tok-na">getCmmnRuntimeService</span><span class="tok-o">();</span>
<span class="tok-n">CaseInstance</span> <span class="tok-n">caseInstance</span> <span class="tok-o">=</span> <span class="tok-n">cmmnRuntimeService</span><span class="tok-o">.</span><span class="tok-na">createCaseInstanceBuilder</span><span class="tok-o">()</span>
    <span class="tok-o">.</span><span class="tok-na">caseDefinitionKey</span><span class="tok-o">(</span><span class="tok-s">&quot;employeeOnboarding&quot;</span><span class="tok-o">)</span>
    <span class="tok-o">.</span><span class="tok-na">variable</span><span class="tok-o">(</span><span class="tok-s">&quot;potentialEmployee&quot;</span><span class="tok-o">,</span> <span class="tok-s">&quot;johnDoe&quot;</span><span class="tok-o">)</span>
    <span class="tok-o">.</span><span class="tok-na">start</span><span class="tok-o">();</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>当 <strong>CaseInstance</strong> 启动后, 引擎将决定哪一个计划项应该被激活:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>第一个阶段没有入口凭证, 所以它会被激活</p>
</li>
<li>
<p>第一个阶段的子人工任务没有入口凭证, 所以它们三个也预计被激活</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>在运行时, 计划项被当做 <strong>PlanItemInstances</strong>, 可以使用 <em>CmmnRuntimeService</em> 查询获取:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="java"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1
2
3
4
5
6
7
8</div></td><td class="code"><span></span><span class="tok-n">List</span><span class="tok-o">&lt;</span><span class="tok-n">PlanItemInstance</span><span class="tok-o">&gt;</span> <span class="tok-n">planItemInstances</span> <span class="tok-o">=</span> <span class="tok-n">cmmnRuntimeService</span><span class="tok-o">.</span><span class="tok-na">createPlanItemInstanceQuery</span><span class="tok-o">()</span>
    <span class="tok-o">.</span><span class="tok-na">caseInstanceId</span><span class="tok-o">(</span><span class="tok-n">caseInstance</span><span class="tok-o">.</span><span class="tok-na">getId</span><span class="tok-o">())</span>
    <span class="tok-o">.</span><span class="tok-na">orderByName</span><span class="tok-o">().</span><span class="tok-na">asc</span><span class="tok-o">()</span>
    <span class="tok-o">.</span><span class="tok-na">list</span><span class="tok-o">();</span>

<span class="tok-k">for</span> <span class="tok-o">(</span><span class="tok-n">PlanItemInstance</span> <span class="tok-n">planItemInstance</span> <span class="tok-o">:</span> <span class="tok-n">planItemInstances</span><span class="tok-o">)</span> <span class="tok-o">{</span>
    <span class="tok-n">System</span><span class="tok-o">.</span><span class="tok-na">out</span><span class="tok-o">.</span><span class="tok-na">println</span><span class="tok-o">(</span><span class="tok-n">planItemInstance</span><span class="tok-o">.</span><span class="tok-na">getName</span><span class="tok-o">());</span>
<span class="tok-o">}</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>打印如下:</p>
</div>
<div class="listingblock">
<div class="content">
<pre>After starting
Agree start date
Allocate office
Create email address
Prior to starting
Reject job
Send joining letter to candidate</pre>
</div>
</div>
<div class="paragraph">
<p>上述打印结果中有些事情可能出乎意料:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>stages 也是属于 计划项, 同样被当做 <strong>PlanItemInstance</strong>. 注意当调用 <em>.getStageInstanceId()</em> ,子计划项实例也会拥有stage作为父类.</p>
</li>
<li>
<p><em>Send joining letter to candidate</em> 也在返回结果集中.原因是因为依据 CMMN 1.1 规范, 这个计划项实例是出于可用状态, 而不是处于活跃状态.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>进一步, 修改上述代码:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="java"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1
2
3
4
5</div></td><td class="code"><span></span><span class="tok-k">for</span> <span class="tok-o">(</span><span class="tok-n">PlanItemInstance</span> <span class="tok-n">planItemInstance</span> <span class="tok-o">:</span> <span class="tok-n">planItemInstances</span><span class="tok-o">)</span> <span class="tok-o">{</span>
    <span class="tok-n">System</span><span class="tok-o">.</span><span class="tok-na">out</span><span class="tok-o">.</span><span class="tok-na">println</span><span class="tok-o">(</span><span class="tok-n">planItemInstance</span><span class="tok-o">.</span><span class="tok-na">getName</span><span class="tok-o">()</span>
        <span class="tok-o">+</span> <span class="tok-s">&quot;, state=&quot;</span> <span class="tok-o">+</span> <span class="tok-n">planItemInstance</span><span class="tok-o">.</span><span class="tok-na">getState</span><span class="tok-o">()</span>
        <span class="tok-o">+</span> <span class="tok-s">&quot;, parent stage=&quot;</span> <span class="tok-o">+</span> <span class="tok-n">planItemInstance</span><span class="tok-o">.</span><span class="tok-na">getStageInstanceId</span><span class="tok-o">());</span>
<span class="tok-o">}</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>现在输出如下:</p>
</div>
<div class="listingblock">
<div class="content">
<pre>After starting, state=available, parent stage=null
Agree start date, state=active, parent stage=fe37ac97-b016-11e7-b3ad-acde48001122
Allocate office, state=active, parent stage=fe37ac97-b016-11e7-b3ad-acde48001122
Create email address, state=active, parent stage=fe37ac97-b016-11e7-b3ad-acde48001122
Prior to starting, state=active, parent stage=null
Reject job, state=active, parent stage=fe37ac97-b016-11e7-b3ad-acde48001122
Send joining letter to candidate, state=available, parent stage=fe37ac97-b016-11e7-b3ad-acde48001122</pre>
</div>
</div>
<div class="paragraph">
<p>为了只显示活跃状态的计划项实例, 查询可以调整并增加 <em>planItemInstanceStateActive()</em> 方法:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="java"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1
2
3
4
5</div></td><td class="code"><span></span> <span class="tok-n">List</span><span class="tok-o">&lt;</span><span class="tok-n">PlanItemInstance</span><span class="tok-o">&gt;</span> <span class="tok-n">planItemInstances</span> <span class="tok-o">=</span> <span class="tok-n">cmmnRuntimeService</span><span class="tok-o">.</span><span class="tok-na">createPlanItemInstanceQuery</span><span class="tok-o">()</span>
    <span class="tok-o">.</span><span class="tok-na">caseInstanceId</span><span class="tok-o">(</span><span class="tok-n">caseInstance</span><span class="tok-o">.</span><span class="tok-na">getId</span><span class="tok-o">())</span>
    <span class="tok-o">.</span><span class="tok-na">planItemInstanceStateActive</span><span class="tok-o">()</span>
    <span class="tok-o">.</span><span class="tok-na">orderByName</span><span class="tok-o">().</span><span class="tok-na">asc</span><span class="tok-o">()</span>
    <span class="tok-o">.</span><span class="tok-na">list</span><span class="tok-o">();</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>现在输出如下:</p>
</div>
<div class="listingblock">
<div class="content">
<pre>Agree start date
Allocate office
Create email address
Prior to starting
Reject job</pre>
</div>
</div>
<div class="paragraph">
<p>当然, <strong>PlanItemInstance</strong> 是 低级别的表示, 但是每个计划项也拥有一个  <em>plan item definition</em>  明确其类型. 在这个例子中, 我们只有 <em>human tasks</em>.通过使用 计划项实例 来影响整个 案例实例,
(比如, <em>CmmnRuntimeService.triggerPlanItemInstance(String planItemInstanceId)</em>).然而, 交互最有可能通过实际计划项定义的结果发生: 比如这里的人工任务.</p>
</div>
<div class="paragraph">
<p>任务的查询方法和BPMN 引擎一样, (实际上, 任务服务是一个共享的组件, BPMN或者CMMN创建的任务都可以通过各自引擎查询得到):</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="java"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv"> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17</div></td><td class="code"><span></span><span class="tok-n">CmmnTaskService</span> <span class="tok-n">cmmnTaskService</span> <span class="tok-o">=</span> <span class="tok-n">cmmnEngine</span><span class="tok-o">.</span><span class="tok-na">getCmmnTaskService</span><span class="tok-o">();</span>
<span class="tok-n">List</span><span class="tok-o">&lt;</span><span class="tok-n">Task</span><span class="tok-o">&gt;</span> <span class="tok-n">hrTasks</span> <span class="tok-o">=</span> <span class="tok-n">cmmnTaskService</span><span class="tok-o">.</span><span class="tok-na">createTaskQuery</span><span class="tok-o">()</span>
    <span class="tok-o">.</span><span class="tok-na">taskCandidateGroup</span><span class="tok-o">(</span><span class="tok-s">&quot;hr&quot;</span><span class="tok-o">)</span>
    <span class="tok-o">.</span><span class="tok-na">caseInstanceId</span><span class="tok-o">(</span><span class="tok-n">caseInstance</span><span class="tok-o">.</span><span class="tok-na">getId</span><span class="tok-o">())</span>
    <span class="tok-o">.</span><span class="tok-na">orderByTaskName</span><span class="tok-o">().</span><span class="tok-na">asc</span><span class="tok-o">()</span>
    <span class="tok-o">.</span><span class="tok-na">list</span><span class="tok-o">();</span>
<span class="tok-k">for</span> <span class="tok-o">(</span><span class="tok-n">Task</span> <span class="tok-n">task</span> <span class="tok-o">:</span> <span class="tok-n">hrTasks</span><span class="tok-o">)</span> <span class="tok-o">{</span>
    <span class="tok-n">System</span><span class="tok-o">.</span><span class="tok-na">out</span><span class="tok-o">.</span><span class="tok-na">println</span><span class="tok-o">(</span><span class="tok-s">&quot;Task for HR : &quot;</span> <span class="tok-o">+</span> <span class="tok-n">task</span><span class="tok-o">.</span><span class="tok-na">getName</span><span class="tok-o">());</span>
<span class="tok-o">}</span>

<span class="tok-n">List</span><span class="tok-o">&lt;</span><span class="tok-n">Task</span><span class="tok-o">&gt;</span> <span class="tok-n">employeeTasks</span> <span class="tok-o">=</span> <span class="tok-n">cmmnTaskService</span><span class="tok-o">.</span><span class="tok-na">createTaskQuery</span><span class="tok-o">()</span>
    <span class="tok-o">.</span><span class="tok-na">taskAssignee</span><span class="tok-o">(</span><span class="tok-s">&quot;johndoe&quot;</span><span class="tok-o">)</span>
    <span class="tok-o">.</span><span class="tok-na">orderByTaskName</span><span class="tok-o">().</span><span class="tok-na">asc</span><span class="tok-o">()</span>
    <span class="tok-o">.</span><span class="tok-na">list</span><span class="tok-o">();</span>
<span class="tok-k">for</span> <span class="tok-o">(</span><span class="tok-n">Task</span> <span class="tok-n">task</span> <span class="tok-o">:</span> <span class="tok-n">employeeTasks</span><span class="tok-o">)</span> <span class="tok-o">{</span>
    <span class="tok-n">System</span><span class="tok-o">.</span><span class="tok-na">out</span><span class="tok-o">.</span><span class="tok-na">println</span><span class="tok-o">(</span><span class="tok-s">&quot;Task for employee: &quot;</span> <span class="tok-o">+</span> <span class="tok-n">task</span><span class="tok-o">);</span>
<span class="tok-o">}</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>上述输出:</p>
</div>
<div class="listingblock">
<div class="content">
<pre>Task for HR : Agree start date
Task for HR : Allocate office
Task for HR : Create email address

Task for employee: Reject job</pre>
</div>
</div>
<div class="paragraph">
<p>当HR的三个任务都完成, 给求职者发送入职信的任务应该是可用的:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="java"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv"> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13</div></td><td class="code"><span></span><span class="tok-k">for</span> <span class="tok-o">(</span><span class="tok-n">Task</span> <span class="tok-n">task</span> <span class="tok-o">:</span> <span class="tok-n">hrTasks</span><span class="tok-o">)</span> <span class="tok-o">{</span>
    <span class="tok-n">cmmnTaskService</span><span class="tok-o">.</span><span class="tok-na">complete</span><span class="tok-o">(</span><span class="tok-n">task</span><span class="tok-o">.</span><span class="tok-na">getId</span><span class="tok-o">());</span>
<span class="tok-o">}</span>

<span class="tok-n">hrTasks</span> <span class="tok-o">=</span> <span class="tok-n">cmmnTaskService</span><span class="tok-o">.</span><span class="tok-na">createTaskQuery</span><span class="tok-o">()</span>
    <span class="tok-o">.</span><span class="tok-na">taskCandidateGroup</span><span class="tok-o">(</span><span class="tok-s">&quot;hr&quot;</span><span class="tok-o">)</span>
    <span class="tok-o">.</span><span class="tok-na">caseInstanceId</span><span class="tok-o">(</span><span class="tok-n">caseInstance</span><span class="tok-o">.</span><span class="tok-na">getId</span><span class="tok-o">())</span>
    <span class="tok-o">.</span><span class="tok-na">orderByTaskName</span><span class="tok-o">().</span><span class="tok-na">asc</span><span class="tok-o">()</span>
    <span class="tok-o">.</span><span class="tok-na">list</span><span class="tok-o">();</span>

<span class="tok-k">for</span> <span class="tok-o">(</span><span class="tok-n">Task</span> <span class="tok-n">task</span> <span class="tok-o">:</span> <span class="tok-n">hrTasks</span><span class="tok-o">)</span> <span class="tok-o">{</span>
    <span class="tok-n">System</span><span class="tok-o">.</span><span class="tok-na">out</span><span class="tok-o">.</span><span class="tok-na">println</span><span class="tok-o">(</span><span class="tok-s">&quot;Task for HR : &quot;</span> <span class="tok-o">+</span> <span class="tok-n">task</span><span class="tok-o">.</span><span class="tok-na">getName</span><span class="tok-o">());</span>
<span class="tok-o">}</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>事实上, 预期的任务现在被创建了:</p>
</div>
<div class="listingblock">
<div class="content">
<pre>Task for HR : Send joining letter to candidate</pre>
</div>
</div>
<div class="paragraph">
<p>完成这个任务,案例实例将进入第二个阶段, 同时第一阶段的哨兵的条件得到满足. 'Reject job&#8217;任务被程序自动创建, 并且指派给员工的两个任务也被创建:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="java"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1
2
3</div></td><td class="code"><span></span><span class="tok-n">Task</span> <span class="tok-k">for</span> <span class="tok-n">employee</span><span class="tok-o">:</span> <span class="tok-n">Fill</span> <span class="tok-n">in</span> <span class="tok-n">paperwork</span>
<span class="tok-n">Task</span> <span class="tok-k">for</span> <span class="tok-n">employee</span><span class="tok-o">:</span> <span class="tok-n">New</span> <span class="tok-n">starter</span> <span class="tok-n">training</span>
<span class="tok-n">Task</span> <span class="tok-k">for</span> <span class="tok-n">employee</span><span class="tok-o">:</span> <span class="tok-n">Reject</span> <span class="tok-n">job</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>完成所有任务将结束整个案例实例:</p>
</div>
<div class="listingblock">
<div class="content">
<pre>List&lt;Task&gt; tasks = cmmnTaskService.createTaskQuery().caseInstanceId(caseInstance.getId()).listPage(0, 1);
while (!tasks.isEmpty()) {
    cmmnTaskService.complete(tasks.get(0).getId());
    tasks = cmmnTaskService.createTaskQuery()
        .caseInstanceId(caseInstance.getId())
        .listPage(0, 1);
}</pre>
</div>
</div>
<div class="paragraph">
<p>当执行案例实例时, 引擎也会保存历史信息, 这可以通过查询API获取:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="java"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv"> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16</div></td><td class="code"><span></span><span class="tok-n">CmmnHistoryService</span> <span class="tok-n">cmmnHistoryService</span> <span class="tok-o">=</span> <span class="tok-n">cmmnEngine</span><span class="tok-o">.</span><span class="tok-na">getCmmnHistoryService</span><span class="tok-o">();</span>
<span class="tok-n">HistoricCaseInstance</span> <span class="tok-n">historicCaseInstance</span> <span class="tok-o">=</span> <span class="tok-n">cmmnHistoryService</span><span class="tok-o">.</span><span class="tok-na">createHistoricCaseInstanceQuery</span><span class="tok-o">()</span>
    <span class="tok-o">.</span><span class="tok-na">caseInstanceId</span><span class="tok-o">(</span><span class="tok-n">caseInstance</span><span class="tok-o">.</span><span class="tok-na">getId</span><span class="tok-o">())</span>
    <span class="tok-o">.</span><span class="tok-na">singleResult</span><span class="tok-o">();</span>

<span class="tok-n">System</span><span class="tok-o">.</span><span class="tok-na">out</span><span class="tok-o">.</span><span class="tok-na">println</span><span class="tok-o">(</span><span class="tok-s">&quot;Case instance execution took &quot;</span>
    <span class="tok-o">+</span> <span class="tok-o">(</span><span class="tok-n">historicCaseInstance</span><span class="tok-o">.</span><span class="tok-na">getEndTime</span><span class="tok-o">().</span><span class="tok-na">getTime</span><span class="tok-o">()</span> <span class="tok-o">-</span> <span class="tok-n">historicCaseInstance</span><span class="tok-o">.</span><span class="tok-na">getStartTime</span><span class="tok-o">().</span><span class="tok-na">getTime</span><span class="tok-o">())</span> <span class="tok-o">+</span> <span class="tok-s">&quot; ms&quot;</span><span class="tok-o">);</span>

<span class="tok-n">List</span><span class="tok-o">&lt;</span><span class="tok-n">HistoricTaskInstance</span><span class="tok-o">&gt;</span> <span class="tok-n">historicTaskInstances</span> <span class="tok-o">=</span> <span class="tok-n">cmmnHistoryService</span><span class="tok-o">.</span><span class="tok-na">createHistoricTaskInstanceQuery</span><span class="tok-o">()</span>
    <span class="tok-o">.</span><span class="tok-na">caseInstanceId</span><span class="tok-o">(</span><span class="tok-n">caseInstance</span><span class="tok-o">.</span><span class="tok-na">getId</span><span class="tok-o">())</span>
    <span class="tok-o">.</span><span class="tok-na">orderByTaskCreateTime</span><span class="tok-o">().</span><span class="tok-na">asc</span><span class="tok-o">()</span>
    <span class="tok-o">.</span><span class="tok-na">list</span><span class="tok-o">();</span>

<span class="tok-k">for</span> <span class="tok-o">(</span><span class="tok-n">HistoricTaskInstance</span> <span class="tok-n">historicTaskInstance</span> <span class="tok-o">:</span> <span class="tok-n">historicTaskInstances</span><span class="tok-o">)</span> <span class="tok-o">{</span>
    <span class="tok-n">System</span><span class="tok-o">.</span><span class="tok-na">out</span><span class="tok-o">.</span><span class="tok-na">println</span><span class="tok-o">(</span><span class="tok-s">&quot;Task completed: &quot;</span> <span class="tok-o">+</span> <span class="tok-n">historicTaskInstance</span><span class="tok-o">.</span><span class="tok-na">getName</span><span class="tok-o">());</span>
<span class="tok-o">}</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>输出如下:</p>
</div>
<div class="listingblock">
<div class="content">
<pre>Case instance execution took 149 ms
Task completed: Reject job
Task completed: Agree start date
Task completed: Allocate office
Task completed: Create email address
Task completed: Send joining letter to candidate
Task completed: New starter training
Task completed: Fill in paperwork</pre>
</div>
</div>
<div class="paragraph">
<p>案例执行相关的历史数据以特殊的结构被收集, 比如 Tasks (上面所见的), milestones, cases, variables and 一般的 plan items.
这个数据作为运行时数据被持久化, 但是不会在实例结束后被删除.访问历史数据可以通过 <em>CmmnHistoryService</em> 的相关API</p>
</div>
<div class="paragraph">
<p>当然, 这只是Flowable CMMN Engine所有可用的APIs中的一小部分,请查阅其他章节了解更多详情</p>
</div>
</div>
<div class="sect2">
<h3 id="_cmmn_1_1_constructs">6.4. CMMN 1.1 Constructs</h3>
<div class="paragraph">
<p>这个小节 覆盖了 Flowable支持的 CMMN 1.1设计, 同时也是对 CMMN 1.1 标准的拓展</p>
</div>
<div class="paragraph">
<p>下述设计, 除了哨兵和item 控制, 都是依据 CMMN规范中的计划项作为参考.它们实例执行的历史数据可用通过 <em>CmmnHistoryService</em> 使用 <em>org.flowable.cmmn.api.history.HistoricPlanItemInstanceQuery</em> 方法获取.</p>
</div>
<div class="sect3">
<h4 id="_阶段">6.4.1. 阶段</h4>
<div class="paragraph">
<p>一个 stage 被看作一组计划项的集合, 它通常用于在案例实例中定义"阶段"</p>
</div>
<div class="paragraph">
<p>一个 stage 本事也是计划项, 也拥有进入和退出凭证. 计划项以及它包含的计划项只有在计入活跃状态才可用, stage 可以内嵌在其他stage中.
A stage is visualized as a rectangle with angled corners:</p>
</div>
<div class="paragraph">
<p>a stage 可以看作是一个有尖角的矩形</p>
</div>
<div class="imageblock" style="text-align: center">
<div class="content">
<img src="images/cmmn.stage.png" alt="cmmn.stage">
</div>
</div>
</div>
<div class="sect3">
<h4 id="_任务">6.4.2. 任务</h4>
<div class="paragraph">
<p>一个手动任务, 意味着它将在引擎外部发生.</p>
</div>
<div class="paragraph">
<p>Properties:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><strong>name</strong>: 表达式将会在运行时被解析当做任务名称</p>
</li>
<li>
<p><strong>blocking</strong>: 布尔值决定是否任务被阻塞</p>
</li>
<li>
<p><strong>blockingExpression</strong>: 表达式 计算成布尔值决定该任务是否被阻塞</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>如果一个任务是 非阻塞non-blocking, 引擎将在执行时自动完成它. 如果一个任务被阻塞blocking, 这个任务对应的计划项实例会被保持活跃状态active state,
直到以编程方式触发( <em>CmmnRuntimeService.triggerPlanItemInstance(String planItemInstanceId)</em> 方法).</p>
</div>
<div class="paragraph">
<p>一个任务看作一个圆角矩形:</p>
</div>
<div class="imageblock" style="text-align: center">
<div class="content">
<img src="images/cmmn.task.png" alt="cmmn.task">
</div>
</div>
</div>
<div class="sect3">
<h4 id="_人工任务">6.4.3. 人工任务</h4>
<div class="paragraph">
<p>一个人工任务用作模型化需要人完成的工作, 比如表单. 当引擎抵达一个人工任务, 被指派的人工或组对应的任务列表就会新增加一项.</p>
</div>
<div class="paragraph">
<p>一个人工任务也是一个计划项, 这意味着除了人工任务本身之外，还创建了一个 <em>PlanItemInstance</em>,并且可以通过 <em>PlanItemInstanceQuery</em> 查询它.</p>
</div>
<div class="paragraph">
<p>人工任务可以通过 <em>org.flowable.task.api.TaskQuery</em> API 查询.历史数据可以通过 <em>org.flowable.task.api.history.HistoricTaskInstanceQuery</em> 查询.</p>
</div>
<div class="paragraph">
<p>Properties:</p>
</div>
<div class="paragraph">
<p>属性:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><strong>name</strong>: 被用作该人工任务的名称</p>
</li>
<li>
<p><strong>blocking</strong>: 布尔值决定该任务是否被阻塞</p>
</li>
<li>
<p><strong>blockingExpression</strong>: 表达式计算得到布尔值决定任务是否被阻塞</p>
</li>
<li>
<p><strong>assignee</strong> : 表达式(可以是静态文本) 决定该任务的指派人</p>
</li>
<li>
<p><strong>owner</strong> : 表达式(可以是静态文本)决定该任务的拥有者</p>
</li>
<li>
<p><strong>candidateUsers</strong> : 表达式(可以是静态文本)解析成以逗号分隔的字符串,被用作决定该人工任务的候选人列表</p>
</li>
<li>
<p><strong>candidateGroups</strong> : 表达式(可以是静态文本)解析成以逗号分隔的字符串,被用作决定该人工任务的候选组列表</p>
</li>
<li>
<p><strong>form key</strong>: 表达式决定使用表单的key, 后续通过API访问</p>
</li>
<li>
<p><strong>Due date</strong> 过期时间 解析为 java.util.Date or a ISO-8601 date string</p>
</li>
<li>
<p><strong>Priority</strong>: 优先级 解析为整型, 可以用来在 TaskQuery API中筛选任务</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>一个人工任务看作一个圆角矩形, 左上角有一个用户图标</p>
</div>
<div class="imageblock" style="text-align: center">
<div class="content">
<img src="images/cmmn.humantask.png" alt="cmmn.humantask">
</div>
</div>
</div>
<div class="sect3">
<h4 id="_java_服务任务">6.4.4. Java 服务任务</h4>
<div class="paragraph">
<p>服务任务被用作执行自定义逻辑.</p>
</div>
<div class="paragraph">
<p>自定义逻辑要放在一个实现 <em>org.flowable.cmmn.api.delegate.PlanItemJavaDelegate</em> 接口的类中.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="java"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1
2
3
4
5
6
7
8</div></td><td class="code"><span></span><span class="tok-kd">public</span> <span class="tok-kd">class</span> <span class="tok-nc">MyJavaDelegate</span> <span class="tok-kd">implements</span> <span class="tok-n">PlanItemJavaDelegate</span> <span class="tok-o">{</span>

    <span class="tok-kd">public</span> <span class="tok-kt">void</span> <span class="tok-nf">execute</span><span class="tok-o">(</span><span class="tok-n">DelegatePlanItemInstance</span> <span class="tok-n">planItemInstance</span><span class="tok-o">)</span> <span class="tok-o">{</span>
        <span class="tok-n">String</span> <span class="tok-n">value</span> <span class="tok-o">=</span> <span class="tok-o">(</span><span class="tok-n">String</span><span class="tok-o">)</span> <span class="tok-n">planItemInstance</span><span class="tok-o">.</span><span class="tok-na">getVariable</span><span class="tok-o">(</span><span class="tok-s">&quot;someVariable&quot;</span><span class="tok-o">);</span>
        <span class="tok-o">...</span>
    <span class="tok-o">}</span>

<span class="tok-o">}</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>对于一些高级实现,使用 <em>PlanItemJavaDelegate</em> 可能不能覆盖到, <em>CmmnActivityBehavior_可以被使用(类似于 BPMN 引擎中的 _JavaDelegate</em> vs <em>ActivityBehavior</em>)</p>
</div>
<div class="paragraph">
<p>Properties:</p>
</div>
<div class="paragraph">
<p>属性:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><strong>name</strong>: 服务任务service task的名称</p>
</li>
<li>
<p><strong>class</strong>: 自定义逻辑的Java实现类</p>
</li>
<li>
<p><strong>class fields</strong>: 调用自定义逻辑时的传递参数</p>
</li>
<li>
<p><strong>Delegate expression</strong>: 表达式解析为一个实现_PlanItemJavaDelegate_接口的类</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>一个 服务任务service task 看作一个圆角矩形, 左上角有个齿轮图标</p>
</div>
<div class="imageblock" style="text-align: center">
<div class="content">
<img src="images/cmmn.servicetask.png" alt="cmmn.servicetask">
</div>
</div>
<div class="paragraph">
<p>====决策任务</p>
</div>
<div class="paragraph">
<p>一个决策任务调用外部 DMN 决策表，并在case实例中存储结果变量</p>
</div>
<div class="paragraph">
<p>Properties:</p>
</div>
<div class="paragraph">
<p>属性:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><strong>Decision table reference</strong>: 相关的需要被执行 DMN 决策表.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>通过设置'<em>Throw error if no rules were hit</em>'属性, 当在 DMN 决策表计算过程中没有命中任何规则时, 可能会抛出错误.</p>
</div>
<div class="paragraph">
<p>一个 服务任务service task 看作一个圆角矩形, 左上角有个表格图标</p>
</div>
<div class="imageblock" style="text-align: center">
<div class="content">
<img src="images/cmmn.decisiontask.png" alt="cmmn.decisiontask">
</div>
</div>
</div>
<div class="sect3">
<h4 id="_http请求任务">6.4.5. http请求任务</h4>
<div class="paragraph">
<p>Http Task http请求任务是 <em>service task</em> 一个开箱即用的实现,被用作调用一个http REST 服务.</p>
</div>
<div class="paragraph">
<p>Http Task http请求任务包含多个参数来自定义请求和响应. 查阅  BPMN http task documentation 了解更多参数设置的细节</p>
</div>
<div class="paragraph">
<p>一个 服务任务service task 看作一个圆角矩形, 左上角有个火箭图标</p>
</div>
<div class="imageblock" style="text-align: center">
<div class="content">
<img src="images/cmmn.httptask.png" alt="cmmn.httptask">
</div>
</div>
</div>
<div class="sect3">
<h4 id="_脚本任务">6.4.6. 脚本任务</h4>
<div class="paragraph">
<p>脚本任务类似 BPMN 中脚本任务, 当一个计划项变成活跃状态, 用来执行一个脚本.</p>
</div>
<div class="paragraph">
<p>Properties:</p>
</div>
<div class="paragraph">
<p>属性:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><strong>name</strong>: 表示任务名称</p>
</li>
<li>
<p><strong>type</strong>: 任务属性, 必须是"script", 表示该任务类型</p>
</li>
<li>
<p><strong>scriptFormat</strong>: 拓展属性 表示脚本语言(例如, javascript, groovy)</p>
</li>
<li>
<p><strong>script</strong>: 执行的脚本, 在"script"元素中作为一个string</p>
</li>
<li>
<p><strong>autoStoreVariables</strong>: 可选的任务属性标记 (默认: false) 表示脚本中定义的变量是否保存到计划项实例上下文中 (查看下面注意事项note)</p>
</li>
<li>
<p><strong>resultVariableName</strong>: 可选的任务属性 明确脚本执行结果保存到计划项实例上下文中对应的名称 (查看下面注意事项note)</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>一个 服务任务service task 看作一个圆角矩形, 左上角有个脚本图标</p>
</div>
<div class="imageblock" style="text-align: center">
<div class="content">
<img src="images/cmmn.scripttask.png" alt="cmmn.scripttask">
</div>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="xml"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv"> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14</div></td><td class="code"><span></span><span class="tok-nt">&lt;planItem</span> <span class="tok-na">id=</span><span class="tok-s">&quot;scriptPlanItem&quot;</span> <span class="tok-na">name=</span><span class="tok-s">&quot;Script Plan Item&quot;</span> <span class="tok-na">definitionRef=</span><span class="tok-s">&quot;myScriptTask&quot;</span> <span class="tok-nt">/&gt;</span>
<span class="tok-nt">&lt;task</span> <span class="tok-na">name=</span><span class="tok-s">&quot;My Script Task Item&quot;</span> <span class="tok-na">flowable:type=</span><span class="tok-s">&quot;script&quot;</span> <span class="tok-na">flowable:scriptFormat=</span><span class="tok-s">&quot;JavaScript&quot;</span><span class="tok-nt">&gt;</span>
    <span class="tok-nt">&lt;documentation&gt;</span>Optional documentation<span class="tok-nt">&lt;/documentation&gt;</span>
    <span class="tok-nt">&lt;extensionElements&gt;</span>
        <span class="tok-nt">&lt;flowable:field</span> <span class="tok-na">name=</span><span class="tok-s">&quot;script&quot;</span><span class="tok-nt">&gt;</span>
            <span class="tok-nt">&lt;string&gt;</span>
                sum = 0;
                for ( i in inputArray ) {
                    sum += i;
                }
            <span class="tok-nt">&lt;/string&gt;</span>
        <span class="tok-nt">&lt;/flowable:field&gt;</span>
    <span class="tok-nt">&lt;/extensionElements&gt;</span>
<span class="tok-nt">&lt;/task&gt;</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p><strong>Note</strong>: <strong>scriptFormat</strong> 属性值必须符合 <a href="http://jcp.org/en/jsr/detail?id=223">JSR-223</a> (scripting for the Java platform).
默认, JavaScript 包含在每个JDK里, 不需要额外的JAR文件. 如果你想使用另外 (JSR-223 兼容) 脚本引擎, 在classpath中添加对应的Jar文件并使用合适的名称.
例如, Flowable 单元测试 经常使用 Groovy, 因为其语法和 JAVA 相像.</p>
</div>
<div class="paragraph">
<p>注意Groovy脚本引擎是绑定在groovy-jsr223 JAR, 这样, 必须添加下面的依赖:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="xml"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1
2
3
4
5</div></td><td class="code"><span></span><span class="tok-nt">&lt;dependency&gt;</span>
    <span class="tok-nt">&lt;groupId&gt;</span>org.codehaus.groovy<span class="tok-nt">&lt;/groupId&gt;</span>
    <span class="tok-nt">&lt;artifactId&gt;</span>groovy-jsr223<span class="tok-nt">&lt;/artifactId&gt;</span>
    <span class="tok-nt">&lt;version&gt;</span>2.x.x<span class="tok-nt">&lt;version&gt;</span>
<span class="tok-nt">&lt;/dependency&gt;</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>在脚本任务中, 所有案例变量 variables 都可以通过 PlanItem 实例在脚本中访问.在下面的例子中, 脚本变量 <em>'inputArray'</em> 实际上是一个案例变量variable(一个整型数组)</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="xml"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1
2
3
4
5
6
7
8</div></td><td class="code"><span></span><span class="tok-nt">&lt;flowable:field</span> <span class="tok-na">name=</span><span class="tok-s">&quot;script&quot;</span><span class="tok-nt">&gt;</span>
    <span class="tok-nt">&lt;string&gt;</span>
    sum = 0
    for ( i in inputArray ) {
      sum += i
    }
    <span class="tok-nt">&lt;/string&gt;</span>
<span class="tok-nt">&lt;/flowable:field&gt;</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p><strong>Note</strong>: 通过在脚本中调用 <em>planItemInstance.setVariable("variableName", variableValue)</em>, 可以设置计划项实例的变量值.默认不会保存任务变量.
通过设置 <em>autoStoreVariables_属性值为true, 也可以自动保存在脚本中定义的变量(例如, 上述实例中的sum).然而, 最好是不要显示调用 _planItemInstance.setVariable("variableName", variableValue)</em>
,在某些版本的JDK中, 一些脚本语言自动保存变量会不起作用. 查阅 <a href="http://www.jorambarrez.be/blog/2013/03/25/bug-on-jdk-1-7-0_17-when-using-scripttask-in-activiti/">链接</a> 了解更多细节</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="xml"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1</div></td><td class="code"><span></span><span class="tok-nt">&lt;task</span> <span class="tok-na">name=</span><span class="tok-s">&quot;Script Task&quot;</span> <span class="tok-na">flowable:type=</span><span class="tok-s">&quot;script&quot;</span> <span class="tok-na">flowable:scriptFormat=</span><span class="tok-s">&quot;groovy&quot;</span> <span class="tok-na">flowable:autoStoreVariables=</span><span class="tok-s">&quot;false&quot;</span><span class="tok-nt">&gt;</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>The default for this parameter is <code>false</code>, meaning that if the parameter is omitted from the script task definition, all the declared variables will only exist during the duration of the script.</p>
</div>
<div class="paragraph">
<p>该参数默认是false, 意味着在脚本定义中省略, 所有脚本中声明的变量只会存在于脚本执行过程中.</p>
</div>
<div class="paragraph">
<p>这是一个在脚本中如何设置变量的例子:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="xml"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1
2
3
4
5
6</div></td><td class="code"><span></span><span class="tok-nt">&lt;flowable:field</span> <span class="tok-na">name=</span><span class="tok-s">&quot;script&quot;</span><span class="tok-nt">&gt;</span>
    <span class="tok-nt">&lt;string&gt;</span>
    def scriptVar = &quot;test123&quot;
    planItemInstance.setVariable(&quot;myVar&quot;, scriptVar)
    <span class="tok-nt">&lt;/string&gt;</span>
<span class="tok-nt">&lt;/flowable:field&gt;</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>以下名称是预留的, 不能被使用: <strong>out, out:print, lang:import, context, elcontext</strong>.</p>
</div>
<div class="paragraph">
<p><strong>Note</strong> 脚本任务的返回值可以被分配给一个已经存在或者新的计划项实例变量, 通过在脚本任务定义中设置_'flowable:resultVariable'_ 属性值.任何已经存在的相同名称变量的值会被脚本返回值覆盖.当未明确设置返回结果变量名, 脚本的返回值会被忽略.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="xml"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1
2
3
4
5</div></td><td class="code"><span></span><span class="tok-nt">&lt;task</span> <span class="tok-na">name=</span><span class="tok-s">&quot;Script Task&quot;</span> <span class="tok-na">flowable:type=</span><span class="tok-s">&quot;script&quot;</span> <span class="tok-na">flowable:scriptFormat=</span><span class="tok-s">&quot;groovy&quot;</span> <span class="tok-na">flowable:resultVariable=</span><span class="tok-s">&quot;myVar&quot;</span><span class="tok-nt">&gt;</span>
    <span class="tok-nt">&lt;flowable:field</span> <span class="tok-na">name=</span><span class="tok-s">&quot;script&quot;</span><span class="tok-nt">&gt;</span>
        <span class="tok-nt">&lt;string&gt;</span>#{echo}<span class="tok-nt">&lt;/string&gt;</span>
    <span class="tok-nt">&lt;/flowable:field&gt;</span>
<span class="tok-nt">&lt;/task&gt;</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>在上述实例中, 当脚本执行结束后, 脚本的返回值( <em>'#{echo}'</em>的解析值)会被设置成名称为 <em>'myVar'</em> 的流程变量值.</p>
</div>
</div>
<div class="sect3">
<h4 id="_里程碑">6.4.7. 里程碑</h4>
<div class="paragraph">
<p>里程碑被用作标记到达案例实例中的某个点. 在运行时, 它们被称为 <strong>MilestoneInstances</strong> , 可以通过调用 <em>CmmnRuntimeService</em> 的 <strong>MilestoneInstanceQuery</strong> 查询. 也可以通过 <em>CmmnHistoryService</em> 获取一个历史副本.</p>
</div>
<div class="paragraph">
<p>里程碑也是计划项, 这意味着除了里程碑条目外, <em>PlanItemInstance</em> 同样会被创建, 通过_PlanItemInstanceQuery_ 可以查询到.</p>
</div>
<div class="paragraph">
<p>Properties:</p>
</div>
<div class="paragraph">
<p>属性:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><strong>name</strong>: 一个表达式或者静态文本, 决定里程碑名称</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>里程碑被看作一个圆角矩形(比任务更圆一些)</p>
</div>
<div class="imageblock" style="text-align: center">
<div class="content">
<img src="images/cmmn.milestone.png" alt="cmmn.milestone">
</div>
</div>
</div>
<div class="sect3">
<h4 id="_案例任务">6.4.8. 案例任务</h4>
<div class="paragraph">
<p>用例任务用作在一个案例上下文中启动一个子案例. <em>CaseInstanceQuery</em> 有一些选项查询父类案例.</p>
</div>
<div class="paragraph">
<p>当一个案例任务被阻塞, <em>PlanItemInstance</em> 将会处于活跃状态, 直到子案例全部完成.如果案例任务是非阻塞, 子案例启动后, 计划项实例自动完成.当子案例实例结束对父类没有影响.</p>
</div>
<div class="paragraph">
<p>Properties:</p>
</div>
<div class="paragraph">
<p>属性:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><strong>name</strong>: 表达式或静态文本, 决定名称</p>
</li>
<li>
<p><strong>blocking</strong>: 布尔值决定任务是否被阻塞</p>
</li>
<li>
<p><strong>blockingExpression</strong>: 表达式计算得到布尔值决定任务是否被阻塞</p>
</li>
<li>
<p><strong>Case reference</strong>: 案例定义的key, 用来启动一个子实例.可以是一个表达式</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>一个案例任务看作一个圆角矩形, 左上角有一个case 图标</p>
</div>
<div class="imageblock" style="text-align: center">
<div class="content">
<img src="images/cmmn.casetask.png" alt="cmmn.casetask">
</div>
</div>
</div>
<div class="sect3">
<h4 id="_流程任务">6.4.9. 流程任务</h4>
<div class="paragraph">
<p>流程任务被用作在案例上下文中启动一个流程实例</p>
</div>
<div class="paragraph">
<p>当流程任务被阻塞, <em>PlanItemInstance</em> 将会一直 活跃_active_状态, 直到 流程实例全部完成. 如果流程任务非阻塞, 流程实例任务启动并且计划项实例自动完成, 当流程实例结束对父类没有任何影响.</p>
</div>
<div class="paragraph">
<p>Properties:</p>
</div>
<div class="paragraph">
<p>属性:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><strong>name</strong>: 表达式或静态文本, 决定名称</p>
</li>
<li>
<p><strong>blocking</strong>: 布尔值决定任务是否被阻塞</p>
</li>
<li>
<p><strong>blockingExpression</strong>: 表达式计算得到布尔值决定任务是否被阻塞</p>
</li>
<li>
<p><strong>Process reference</strong>: 案例定义的key, 用来启动一个子实例.可以是一个表达式</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>一个流程任务看作一个圆角矩形, 左上角有个箭头图标</p>
</div>
<div class="imageblock" style="text-align: center">
<div class="content">
<img src="images/cmmn.processtask.png" alt="cmmn.processtask">
</div>
</div>
<div class="paragraph">
<p>流程任务可以配置为具有内部和外部参数，这些参数的形式是_source/sourceExpression_ and <em>target/targetExpression</em>.</p>
</div>
<div class="paragraph">
<p>内部参数在当前案例实例上下文内被解析.</p>
</div>
<div class="ulist">
<ul>
<li>
<p><em>source</em> value 将把一个case实例变量, 映射成一个流程变量</p>
</li>
<li>
<p>或者, the <em>sourceExpression</em> 允许创建任意值，其中表达式根据case实例解析.</p>
</li>
<li>
<p><em>target</em> 是流程变量的名称, 是被_source_映射</p>
</li>
<li>
<p>或者, _targetExpression_将被解析成一个 <strong>string</strong>, 用作流程实例的变量名称.表达式根据case实例上下文解析.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>内部参数在当前流程实例(全局)上下文内被解析.</p>
</div>
<div class="ulist">
<ul>
<li>
<p>The <em>source</em> value will be the process instance variable which value will be mapped to a case variable</p>
</li>
<li>
<p><em>source</em> value 将把一个流程变量值, 映射成一个case变量</p>
</li>
<li>
<p>或者, the <em>sourceExpression</em> 允许创建任意值，其中表达式根据流程实例解析.</p>
</li>
<li>
<p><em>target</em> 是流程变量的名称, 是被_source_映射</p>
</li>
<li>
<p>或者, _targetExpression_将被解析成一个 <strong>string</strong>, 用作case实例的变量名称.表达式根据流程实例(全局)上下文解析.</p>
</li>
</ul>
</div>
</div>
<div class="sect3">
<h4 id="_凭证">6.4.10. 凭证</h4>
<div class="sect4">
<h5 id="_入口凭证_入口哨兵">入口凭证 (入口哨兵)</h5>
<div class="paragraph">
<p>进入凭证构成一个计划项实例的哨兵, 它们由两部分组成:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>One or more parts that depend on other plan items: these define dependencies on state transitions of other plan items. For example, one human task can depend on the state transition <em>complete</em> of three other human tasks to become active itself</p>
</li>
<li>
<p>一个或者多个部分依赖其他计划项: 它们依赖其他计划项状态的转变.例如一个人工任务取决于另外三个人工任务的完成,才能变成活跃状态.</p>
</li>
<li>
<p>One optional <em>if part</em> or <em>condition</em>: this is an expression that allows the definition of a complex condition
*一个可选的如果部分或条件: 允许定义一个复杂条件的表达式</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>A sentry is satisfied when all its criteria are resolved to <em>true</em>. When a criterion evaluates to true, this is stored and remembered for future evaluations. Note that entry criteria of all plan item instances in the <em>available</em> state are evaluated whenever something changes in the case instance.
Multiple sentries are possible on a plan item. However, when one is satisfied, the plan item moves from state <em>available</em> to <em>active</em>.</p>
</div>
<div class="paragraph">
<p>一个哨兵只有所有凭证解析为 <em>true</em>, 才会满足条件. 当一个凭证被解析为true, 将会被存储以便后续计算. 注意当case实例中发生更改时，将计算处于可用状态的所有计划项实例的入口条件.
多个哨兵可能会用在一个计划项, 然而当一个哨兵满足条件, 该计划项状态会从可用 <em>available</em> 变成活跃 <em>active</em>.</p>
</div>
<div class="paragraph">
<p>See <a href="#cmmn_sentry_evaluation">the section on sentry evaluation</a> for more information.</p>
</div>
<div class="paragraph">
<p>查阅 <a href="#cmmn_sentry_evaluation">the section on sentry evaluation</a>, 了解更多详情.</p>
</div>
<div class="paragraph">
<p>An entry criterion is visualized as a diamond shape (white color inside) on the border of a plan item:</p>
</div>
<div class="paragraph">
<p>入口哨兵被看作一个菱形(内部白色),位于计划项边界上:</p>
</div>
<div class="imageblock" style="text-align: center">
<div class="content">
<img src="images/cmmn.entrycriteria.png" alt="cmmn.entrycriteria">
</div>
</div>
</div>
<div class="sect4">
<h5 id="_exit_criterion_exit_sentry">Exit criterion (exit sentry)</h5>

</div>
<div class="sect4">
<h5 id="_exit_criterion_exit_sentry_出口凭证_出口哨兵">Exit criterion (exit sentry) 出口凭证 (出口哨兵)</h5>
<div class="paragraph">
<p>Exit criteria form a sentry for a given plan item instance. They consist of two parts:</p>
</div>
<div class="paragraph">
<p>进入凭证构成一个计划项实例的哨兵, 它们由两部分组成:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>One or more parts that depend on other plan items: these define dependencies on state transitions of other plan items. For example, one human task can depend on reaching a certain milestone to be automatically terminated</p>
</li>
<li>
<p>一个或者多个部分依赖其他计划项: 它们依赖其他计划项状态的转变.例如一个人工任务取决于另外三个人工任务的完成,才能变成活跃状态.</p>
</li>
<li>
<p>One optional <em>if part</em> or <em>condition</em>: this is an expression that allows a complex condition to be defined
*一个可选的如果部分或条件: 允许定义一个复杂条件的表达式</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>A sentry is satisfied when all its criteria are resolved to <em>true</em>. When a criterion evaluates to true, this is stored and remembered for future evaluations. Note that exit criteria of all plan item instances in the <em>active</em> state are evaluated whenever something changes in the case instance.
Multiple sentries are possible on a plan item. However, when one is satisfied, the plan item moves from state <em>active</em> to <em>exit</em>.</p>
</div>
<div class="paragraph">
<p>一个哨兵只有所有凭证解析为 <em>true</em>, 才会满足条件. 当一个凭证被解析为true, 将会被存储以便后续计算. 注意当case实例中发生更改时，将计算处于可用状态的所有计划项实例的入口条件.
多个哨兵可能会用在一个计划项, 然而当一个哨兵满足条件, 该计划项状态会从活跃 <em>active</em> 变成退出  <em>exit</em>.</p>
</div>
<div class="paragraph">
<p>See <a href="#cmmn_sentry_evaluation">the section on sentry evaluation</a> for more information.</p>
</div>
<div class="paragraph">
<p>查阅 <a href="#cmmn_sentry_evaluation">the section on sentry evaluation</a> 了解更多.</p>
</div>
<div class="paragraph">
<p>An exit criterion is visualized as a diamond shape (white color inside) on the border of a plan item:</p>
</div>
<div class="paragraph">
<p>出口哨兵被看作一个菱形(内部黑色),位于计划项边界上:</p>
</div>
<div class="imageblock" style="text-align: center">
<div class="content">
<img src="images/cmmn.exitcriteria.png" alt="cmmn.exitcriteria">
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_event_listeners">6.4.11. Event Listeners</h4>

</div>
<div class="sect3">
<h4 id="_event_listeners_事件监听器">6.4.12. Event Listeners 事件监听器</h4>
<div class="sect4">
<h5 id="_timer_event_listener">Timer Event Listener</h5>

</div>
<div class="sect4">
<h5 id="_timer_event_listener_计时器事件监听器">Timer Event Listener 计时器事件监听器</h5>
<div class="paragraph">
<p>A timer event listener is used when the passing of time needs to be captured in a case model.</p>
</div>
<div class="paragraph">
<p>计时器事件监听器在一个case模型中被用作捕获时间的传递.</p>
</div>
<div class="paragraph">
<p>A timer event listener is not a task and has a simpler plan item lifecycle compared to a <em>task</em>: the timer will simply move from <em>available</em> to <em>completed</em> when the event (in this case, the time passing) occurs.</p>
</div>
<div class="paragraph">
<p>计时器事件监听器不是一个任务, 相比于任务, 拥有更简单的生命周期: 当一个事件(比如, 时间传递)发生时, 计时器简单从可用 <em>available</em> 状态变成完成 <em>completed</em> 状态.</p>
</div>
<div class="paragraph">
<p>Properties:</p>
</div>
<div class="paragraph">
<p>属性:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><strong>Timer expression</strong>: an expression that defines when the timer should occur. The following options are possible:</p>
</li>
<li>
<p><strong>Timer expression</strong>: 表达式定义计时器何时发生. 以下是可能的使用配置:</p>
<div class="ulist">
<ul>
<li>
<p>An expression resolving to a java.util.Date or org.joda.time.DateTime instance (for example, _${someBean.calculateNextDate(someCaseInstanceVariable)})</p>
</li>
<li>
<p>表达式被解析成 a java.util.Date or org.joda.time.DateTime instance (例如,  <em>${someBean.calculateNextDate(someCaseInstanceVariable)}</em>)</p>
</li>
<li>
<p>An ISO8601 date</p>
</li>
<li>
<p>ISO8601 格式日期</p>
</li>
<li>
<p>An ISO8601 duration String (for example, <em>PT5H</em>, indicating the timer should fire in 5 hours from instantiation)</p>
</li>
<li>
<p>ISO8601 格式 持续String (例如, <em>PT5H</em>, 表示计时器应在实例化后5小时启动)</p>
</li>
<li>
<p>AN ISO8601 repetition String (for example, R5/PT2H, indicating the timer should fire 5 times, each time waiting 2 hours)</p>
</li>
<li>
<p>ISO8601 循环时间周期 (例如, R5/PT2H, 表示总共触发5次, 每次间隔2小时)</p>
</li>
<li>
<p>A String containing a cron expression</p>
</li>
<li>
<p>cron表达式指定</p>
</li>
</ul>
</div>
</li>
<li>
<p><strong>Start trigger plan item/event</strong>: reference to a plan item in the case model that triggers the start of the timer event listener</p>
</li>
<li>
<p><strong>Start trigger plan item/event</strong>: 引用case模型中计划项, 用来触发计时器事件监听器的开始.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Note that setting a <em>start trigger</em> for the timer event listener does not have a visual indicator in the case model, unlike entry/exit criteria on sentries.</p>
</div>
<div class="paragraph">
<p>注意 设置计时器事件监听器的 <em>start trigger</em> 属性在case 模型中没有可视化指示, 不像入口/出口哨兵的凭证.</p>
</div>
<div class="paragraph">
<p>A timer event listener is visualized as circle with a clock icon inside:</p>
</div>
<div class="paragraph">
<p>计时器事件监听器看作一个圆, 里面有一个钟表图标:</p>
</div>
<div class="imageblock" style="text-align: center">
<div class="content">
<img src="images/cmmn.timereventlistener.png" alt="cmmn.timereventlistener">
</div>
</div>
</div>
<div class="sect4">
<h5 id="_user_event_listener">User Event Listener</h5>

</div>
<div class="sect4">
<h5 id="_user_event_listener_用户事件监听器">User Event Listener 用户事件监听器</h5>
<div class="paragraph">
<p>A user event listener can be used when needing to capture a user interaction that directly influences a case state,
instead of indirectly via impacting variables or information in the case.
A typical use case for a user event listener are buttons in a UI that a user can click to drive the state of the case instance.
When the event is triggered an <em>Occur</em> event is thrown to which sentries can listener to.
Like timer event listeners, it has a much simpler lifecycle that a <em>task</em>.</p>
</div>
<div class="paragraph">
<p>一个用户事件监听器被用作捕获一个直接影响案例状态而不是间接影响变量或者案例信息的用户交互. 一个典型的例子就是界面的按钮,用户点击后改变case实例的状态.
当 事件被触发, <em>Occur</em> event会被抛出, 并且被哨兵所捕获. 类似计时器事件监听器, 相比任务,它也有一个更为简单的生命周期.</p>
</div>
<div class="imageblock" style="text-align: center">
<div class="content">
<img src="images/cmmn.usereventlistener.png" alt="cmmn.usereventlistener">
</div>
</div>
<div class="paragraph">
<p>User event listeners can be queried using the <em>org.flowable.cmmn.api.runtime.UserEventListenerInstanceQuery</em>. Such a query can be created by calling the <em>cmmnRuntimeService.createUserEventListenerInstanceQuery()</em> method. Note that a user event listener is also a plan item instance, which means it can also be queried through the <em>org.flowable.cmmn.api.runtime.PlanItemInstanceQuery</em> API.</p>
</div>
<div class="paragraph">
<p>用户事件监听器可以使用 <em>org.flowable.cmmn.api.runtime.UserEventListenerInstanceQuery</em> 查询. 类似调用 the <em>cmmnRuntimeService.createUserEventListenerInstanceQuery()</em> 方法.
注意:也是一个计划项实例, 意味着也可以通过 the <em>org.flowable.cmmn.api.runtime.PlanItemInstanceQuery</em> API 查询.</p>
</div>
<div class="paragraph">
<p>A user event listener can be completed by calling the <em>cmmnRuntimeService.completeUserEventListenerInstance(id)</em> method.</p>
</div>
<div class="paragraph">
<p>用户事件监听器可以通过调用 <em>cmmnRuntimeService.completeUserEventListenerInstance(id)</em> 方法来完成.</p>
</div>
</div>
<div class="sect4">
<h5 id="_generic_event_listener">Generic Event Listener</h5>

</div>
<div class="sect4">
<h5 id="_generic_event_listener_一般事件监听器">Generic Event Listener 一般事件监听器</h5>
<div class="paragraph">
<p>A generic event listener is used to typically model a programmatic interaction (e.g. a external system that calls out to change something in a case instance).</p>
</div>
<div class="paragraph">
<p>一般事件监听器通常被用作编程交互建模(例如, 一个外部系统在一个case实例中调用来更改某些东西)</p>
</div>
<div class="imageblock" style="text-align: center">
<div class="content">
<img src="images/cmmn.generic-event-listener.png" alt="cmmn.generic event listener">
</div>
</div>
<div class="paragraph">
<p>The API to retrieve and complete these event listeners is on the <em>CmmnRuntimeService</em>:</p>
</div>
<div class="paragraph">
<p>通过 <em>CmmnRuntimeService</em> 来获取和完成这些事件监听器</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="java"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1
2</div></td><td class="code"><span></span><span class="tok-n">GenericEventListenerInstanceQuery</span> <span class="tok-nf">createGenericEventListenerInstanceQuery</span><span class="tok-o">();</span>
<span class="tok-kt">void</span> <span class="tok-nf">completeGenericEventListenerInstance</span><span class="tok-o">(</span><span class="tok-n">String</span> <span class="tok-n">genericEventListenerInstanceId</span><span class="tok-o">);</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>Similar to <em>user event listeners</em>, this API is a wrapper on top of the <em>PlanItemInstance</em> queries and operations. This means that the data can also be retrieved through the regular <em>PlanItemInstanceQuery</em></p>
</div>
<div class="paragraph">
<p>和  <em>user event listeners</em> 类似, 这个 API 是 <em>PlanItemInstance</em> 查询和操作顶部的包装. 这意味着数据也可以从  <em>PlanItemInstanceQuery</em> 获取.</p>
</div>
<div class="paragraph">
<p>Note that generic event listeners are not part of the CMMN specification, but are a Flowable-specific addition.</p>
</div>
<div class="paragraph">
<p>注意 一般事件监听器不是 CMMN 规范的内容, 而是 Flowable-规范 的补充</p>
</div>
</div>
<div class="sect4">
<h5 id="_automatic_removal_of_event_listeners">Automatic removal of event listeners</h5>

</div>
<div class="sect4">
<h5 id="_automatic_removal_of_event_listeners_自动移除事件监听器">Automatic removal of event listeners 自动移除事件监听器</h5>
<div class="paragraph">
<p>The engine will automatically detect when event listeners (user or timer) are not useful anymore.
Take for example the following case definition:</p>
</div>
<div class="paragraph">
<p>引擎会自动监测不再使用的事件监听器(用户或者定时器), 例如下面的case定义:</p>
</div>
<div class="imageblock" style="text-align: center">
<div class="content">
<img src="images/cmmn.user-event-listener-removal-1.png" alt="cmmn.user event listener removal 1">
</div>
</div>
<div class="paragraph">
<p>Here, the <em>First stage</em> contains two human tasks (A and B) and it can be exited by a user when the <em>Stop first stage</em> user event is triggered.
However, when both taks A and B are completed, the stage will also complete. If now the user event listener would be triggered, there is nothing that listens to this event anymore.
The engine will detect this and terminate the user event automatically.</p>
</div>
<div class="paragraph">
<p>这里, 第一个阶段,包含两个人工任务(A 和 B), 并且当停止第一个阶段事件被触发, 该阶段会被终止. 然而, 当 A 和 B 都完成后, 该阶段stage 也会完成.
如果这个时候触发用户事件监听器, 将会不起任何作用. 此时引擎将会检测到,并自动决定该用户事件.</p>
</div>
<div class="paragraph">
<p>The same mechanism also works for event listeners that are referenced by entry sentries:</p>
</div>
<div class="paragraph">
<p>相同的原理, 适用于两个入口哨兵的监听器场景:</p>
</div>
<div class="imageblock" style="text-align: center">
<div class="content">
<img src="images/cmmn.user-event-listener-removal-2.png" alt="cmmn.user event listener removal 2">
</div>
</div>
<div class="paragraph">
<p>In this case, in the case that <em>EventListenerA</em> would be triggered, <em>EventListenerB</em> is terminated (as nothing is listening to its occurrence anymore).</p>
</div>
<div class="paragraph">
<p>在这种场景, 如果  <em>EventListenerA</em> 被触发, <em>EventListenerB</em> 会终止(因为没有任何在监听B的发生)</p>
</div>
<div class="paragraph">
<p>Or, when timer and user event listeners are mixed, the one that is triggered first will also cause the removal of others (when they are not referenced somewhere else):</p>
</div>
<div class="paragraph">
<p>或者, 当定时器和用户事件监听器被混合使用, 首先被触发的一个会造成其他的移除(当他们没有被别的地方引用)</p>
</div>
<div class="imageblock" style="text-align: center">
<div class="content">
<img src="images/cmmn.user-event-listener-removal-3.png" alt="cmmn.user event listener removal 3">
</div>
</div>
<div class="paragraph">
<p>Here, the timer will be removed in case the user event is triggered first (and vice versa).</p>
</div>
<div class="paragraph">
<p>这里, 定时器会被移除, 如果用户件事监听首先被触发.(反之亦然)</p>
</div>
<div class="paragraph">
<p>The detection also takes in account plan items that have not yet been created. Take for example the following case definition:</p>
</div>
<div class="paragraph">
<p>检测还考虑到尚未创建的计划项.如下图所示:</p>
</div>
<div class="imageblock" style="text-align: center">
<div class="content">
<img src="images/cmmn.user-event-listener-removal-4.png" alt="cmmn.user event listener removal 4">
</div>
</div>
<div class="paragraph">
<p>Here, human task <em>C</em> is not yet created when a case instance is started for this case definition. The user event listener will not be removed as long that <em>C</em> has a parent stage that is in a non-terminal state, as this means that the event could still be listened to in the future.</p>
</div>
<div class="paragraph">
<p>这里, 当case 实例启动后, 人工任务C还未被创建. 只要C具有处于非终止 non-terminal 状态的父级，就不会删除用户事件监听器.这意味着该事件可以在未来依然被监听</p>
</div>
</div>
<div class="sect4">
<h5 id="_available_condition">Available condition</h5>

</div>
<div class="sect4">
<h5 id="_available_condition_可用条件">Available condition 可用条件</h5>
<div class="paragraph">
<p>All types of event listeners can be configured to have a <strong>available condition</strong>: an expressions that will guard the available state of the event listener. To explain the use case, take the following case definition:</p>
</div>
<div class="paragraph">
<p>所有类型的事件监听器都拥有一个 <strong>available condition</strong>: 一个表达式用来维护事件监听器的可用状态.</p>
</div>
<div class="imageblock" style="text-align: center">
<div class="content">
<img src="images/cmmn.create-condition.png" alt="cmmn.create condition">
</div>
</div>
<div class="paragraph">
<p>When the case instance is started, Stage 1 (as it has no entry criteria) will be moving immediately from <em>available</em> to <em>active</em>. Similar story for human task A. Human task B will move from <em>available</em> to <em>enabled</em> as it&#8217;s manually activated.</p>
</div>
<div class="paragraph">
<p>当 case实例启动后, stage 1 (没有入口凭证)将立即从可用状态变成活跃状态. 人工任务A类似. 手动激活人工任务B, 它的状态从 <em>available</em> to <em>enabled</em>.</p>
</div>
<div class="paragraph">
<p>Normally, also the event listener would become <em>available</em>. The life cycle of event listeners is simpler than that of plan items such as human tasks: an event listener stays in the <em>available</em> state until the event happens. There&#8217;s no <em>active</em> state like for other plan items.
This means that a user could trigger it after start and the stage would be exited.</p>
</div>
<div class="paragraph">
<p>正常来讲, 事件监听器状态也会变成 <em>available</em>. 事件监听器的生命周期比其他计划项(比如人工任务)简单: 事件监听器保持可用状态, 直到事件发生. 这里没有类似其他计划项一样拥有活跃状态,
这意味着 一个用户可以在开始之后触发它, 然后stage将会退出.</p>
</div>
<div class="paragraph">
<p>In some use case however, the event listener shouldn&#8217;t be <em>available</em> for the user to interact with (or a timer shouldn&#8217;t start, when using a timer event listener) unless a certain condition is true.</p>
</div>
<div class="paragraph">
<p>在一些场景中,除非一个明确满足true的条件, 否则事件监听器不应该可供用户交互(或者当使用定时器监听时, 一个定时器不应该开始) .</p>
</div>
<div class="paragraph">
<p>In the example above, we want to only create it when the stage doesn&#8217;t have any active children (or required) anymore. Setting the <strong>availableCondition</strong> to <strong>${cmmn:isStageCompletable()}</strong> will allow the event listener to be created which makes it move immediately to <em>available</em>. Concretely in this model, when human task A is completed Stage 1 becomes <em>completable</em> (as human task B is manually activated and non-required). This makes the <em>availbleCondition</em> of the event listener <em>true</em> and the event listener is now available for a user to decide to exit the stage.</p>
</div>
<div class="paragraph">
<p>在上述例子中, 我们想要只在该阶段没有任何活跃元素时创建它. 设置 <strong>availableCondition</strong> to <strong>${cmmn:isStageCompletable()}</strong> 将允许事件监听器被创建, 使得它状态立即变成可用.
在这个模型中具体讲, 当人工任务 A 完成, stage 1 变成可完成(因为人工任务B是手动激活的，并且不是必需的)。这使得事件监听器的可用条件变为true, 同时用户也可以决定是否退出该阶段.</p>
</div>
<div class="paragraph">
<p>Note: this is a Flowable specific addition to the CMMN specification. Without this addition, the event listener would have to be nested within a substage which is protected with entry criteria that listens to the completion of task A.</p>
</div>
<div class="paragraph">
<p>注意: 这是一个 Flowable 规范对CMMN规范的特定添加.如果没有此添加，则事件监听器将必须嵌套在子阶段中，该子阶段受入口凭证监听任务 A 完成的保护。</p>
</div>
<div class="paragraph">
<p>Note: if this were an autocompletable stage, the engine would complete the stage automatically when A completes.</p>
</div>
<div class="paragraph">
<p>注意: 如果是一个可以自动完成的阶段stage, 引擎在A完成时, 自动完成该阶段stage.</p>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_item_control_repetition_rule">6.4.13. Item control: Repetition Rule</h4>

</div>
<div class="sect3">
<h4 id="_item_control_repetition_rule_条目控制_重复规则">6.4.14. Item control: Repetition Rule 条目控制: 重复规则</h4>
<div class="paragraph">
<p>Plan items on the case model can have a <em>repetition rule</em>: an expression that can be used to indicate a certain plan item needs to be repeated.
When no expression is set, but the repetition is enabled (for example, the checkbox is checked in the Flowable Modeler) or the expression is empty, a <em>true</em> value is assumed by default.</p>
</div>
<div class="paragraph">
<p>案例模型中的计划项可以具有重复规则 <em>repetition rule</em> ：一个表达式，可用于指示某个计划项目需要重复。
如果未设置表达式，但启用了重复（例如，在 <em>Flowable Modeler</em> 模型设计器中勾选了复选框）或表达式为空，则默认为 <em>true</em>。</p>
</div>
<div class="paragraph">
<p>An optional <em>repetition counter variable</em> can be set, which holds the index (one-based) of the instance. If not set, the default variable name is <em>repeitionCounter</em>.</p>
</div>
<div class="paragraph">
<p>可以设置一个可选的 <em>repetition counter variable_属性，它将保存一个实例对应的索引(从1开始). 如果未设置,默认的变量名为 _repeitionCounter</em>.</p>
</div>
<div class="paragraph">
<p>If the plan item does not have any entry criteria, the repetition rule expression is evaluated when the plan item is completed or terminated. If the expression resolved to <em>true</em>, a new instance is created. For example, a human task with a repetition rule expression <em>${repetitionCounter &lt; 3}</em>, will create three sequential human tasks.</p>
</div>
<div class="paragraph">
<p>如果一个计划项没有设置入口凭证, 当它完成或则停止时,重复准则表达式都会计算. 如果解析为true, 则创建一个新的实例. 例如, 一个人工人有表达式 <em>${repetitionCounter &lt; 3}</em>, 将会创建3个有序的人工任务.</p>
</div>
<div class="paragraph">
<p>If the plan item has entry criteria, the behavior is different. The repetition rule is not evaluated on completion or termination, but when a sentry of the plan item is satisfied. If both the sentry is satisfied and the repetition rule evaluates to true, a new instance is created.</p>
</div>
<div class="paragraph">
<p>如果计划项有设置入口凭证, 表现是不同的. 当完成或中止时, 重复规则不会计算. 只有满足哨兵的条件才会计算.如果同时满足哨兵条件并且重复准则计算为true, 一个新的实例才会被创建.</p>
</div>
<div class="paragraph">
<p>Take, for example, the following timer event listener followed by a human task. The sentry has one entry criterion for the <em>occur</em> event of the timer event listener. Note that enabling and setting the repetition rule on the task has a visual indicator at the bottom of the rectangle.</p>
</div>
<div class="paragraph">
<p>例如，下面的计时器事件监听器后面跟着一个人工任务。哨兵对于计时器事件监听器的发生事件有一个入口凭证。注意，在任务上启用和设置重复规则都会在矩形底部有一个可视指示器。</p>
</div>
<div class="imageblock" style="text-align: center">
<div class="content">
<img src="images/cmmn.repeatingtimereventlistener.png" alt="cmmn.repeatingtimereventlistener">
</div>
</div>
<div class="paragraph">
<p>If the timer event listener is repeating (for example, <em>R/PT1H</em>), the <em>occur</em> event will be fired every hour. When the repetition rule expression of the human task evaluates to true, a new human task instance will be created each hour.</p>
</div>
<div class="paragraph">
<p>如果定时器事件监听器是可重复的(例如, <em>R/PT1H</em>), 每间隔1小时,发生事件 <em>occur</em> event 会被触发. 当人工任务的重复准则计算为true, 每隔1小时,一个新的人工任务实例会被创建.</p>
</div>
<div class="paragraph">
<p>Note that Flowable allows to have repeating user and generic event listeners. This is contrary to the CMMN specification (which disallows it), but we believe it is needed for having a more flexible way of using event listeners (for example to model a case where a user might multiple times trigger an action that leads to the creation of tasks).</p>
</div>
<div class="paragraph">
<p>注意, Flowable 允许拥有可重复的用户和一般事件监听器. 这和 CMMN 准则是相反的. 但是我们相信拥有一个更加灵活的方式使用时间监听器是需要的(例如在一个用户需要多次触发一个动作来创建人物的案例模型)</p>
</div>
</div>
<div class="sect3">
<h4 id="_item_control_manual_activation_rule">6.4.15. Item control: Manual Activation Rule</h4>

</div>
<div class="sect3">
<h4 id="_item_control_manual_activation_rule_条目控制_手动激活准则">6.4.16. Item control: Manual Activation Rule 条目控制: 手动激活准则</h4>
<div class="paragraph">
<p>Plan items on the case model can have a <em>manual activation rule</em>: an expression that can be used to indicate a certain plan item needs to be <em>manually activated by an end-user</em>.
When no expression is set, but the manual activation is enabled (for example, the checkbox is checked in the Flowable Modeler) or the expression is empty, a <em>true</em> value is assumed by default.</p>
</div>
<div class="paragraph">
<p>案例模型中的计划项可以拥有一个手动激活准则: 一个表达式被提供给终端用户手动激活一个确定的计划项. 当未设置任何表达式, 但是启用了手动激活(例如, Flowable Modeler 中的复选框被选中)
或者表达式为空, 默认值为true.</p>
</div>
<div class="paragraph">
<p>Stages and all task types can be marked for manual activation. Visually, the task or stage will get a <em>play</em> icon (small triangle pointing to the right) to indicate an end-user will have to manually activate it:</p>
</div>
<div class="paragraph">
<p>阶段stage 和所有类型任务都可以设置手动激活. 可视化地, 任务或者阶段stage 将会有一个 播放图标 <em>play</em> icon(一个指向右侧的小三角形),指明用户需要手动去激活它.</p>
</div>
<div class="imageblock" style="text-align: center">
<div class="content">
<img src="images/cmmn.manual-activation.png" alt="cmmn.manual activation">
</div>
</div>
<div class="paragraph">
<p>Normally, when a sentry for a plan item is satisfied (or the plan item doesn&#8217;t have any sentries) the plan item instance is automatically moved to the <em>ACTIVE</em> state. When a manual activation is set though, and it evaluates to true, the plan item instance now becomes <em>ENABLED</em> instead of <em>ACTIVE</em>. As the name implies, the idea behind this is that end-users manually have to activate the plan item instance. A typical use case is showing a list of buttons of potential plan item instances that can currently be started by the end user.</p>
</div>
<div class="paragraph">
<p>正常来讲, 当一个计划项的哨兵条件得到满足(或者没有任何哨兵), 计划项将自动转为活跃状态 <em>ACTIVE</em>. 当设置了手动激活,并且计算结果为true,计划项从活跃状态 <em>ACTIVE</em> 变为已启用状态 <em>ENABLED</em>. 正如名称所示, 背后的想法就是指终端用户需要手动激活计划项.
一个典型的用例就是一个潜在计划项实例的按钮列表.而这些按钮目前可以由最终用户启动.</p>
</div>
<div class="paragraph">
<p>为了启动一个可启动的计划项实例,  可以使用 <em>CmmnRuntimeService</em> 的 <em>startPlanItemInstance</em> 方法:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="java"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1
2
3
4
5
6
7
8</div></td><td class="code"><span></span><span class="tok-n">List</span><span class="tok-o">&lt;</span><span class="tok-n">PlanItemInstance</span><span class="tok-o">&gt;</span> <span class="tok-n">enabledPlanItemInstances</span> <span class="tok-o">=</span> <span class="tok-n">cmmnRuntimeService</span><span class="tok-o">.</span><span class="tok-na">createPlanItemInstanceQuery</span><span class="tok-o">()</span>
    <span class="tok-o">.</span><span class="tok-na">caseInstanceId</span><span class="tok-o">(</span><span class="tok-n">caseInstance</span><span class="tok-o">.</span><span class="tok-na">getId</span><span class="tok-o">())</span>
    <span class="tok-o">.</span><span class="tok-na">planItemInstanceStateEnabled</span><span class="tok-o">()</span>
    <span class="tok-o">.</span><span class="tok-na">list</span><span class="tok-o">();</span>

<span class="tok-c1">// ...</span>

<span class="tok-n">cmmnRuntimeService</span><span class="tok-o">.</span><span class="tok-na">startPlanItemInstance</span><span class="tok-o">(</span><span class="tok-n">planItemInstance</span><span class="tok-o">.</span><span class="tok-na">getId</span><span class="tok-o">());</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>注意,任务的行为只在计划项实例进入活动状态时被执行。例如，对于人工任务，只有在调用 <em>startPlanItemInstance</em> 方法之后才会创建用户任务。</p>
</div>
<div class="paragraph">
<p>已启动的计划项实例可以转变为 禁用状态 <em>DISABLED</em>:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="java"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1</div></td><td class="code"><span></span><span class="tok-n">cmmnRuntimeService</span><span class="tok-o">.</span><span class="tok-na">disablePlanItemInstance</span><span class="tok-o">(</span><span class="tok-n">planItemInstance</span><span class="tok-o">.</span><span class="tok-na">getId</span><span class="tok-o">());</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>Disabled plan item instances can be enabled again:</p>
</div>
<div class="paragraph">
<p>禁用的计划项也可以重新被启动:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="java"><table class="pyhltable"><tr><td class="linenos"><div class="linenodiv">1</div></td><td class="code"><span></span><span class="tok-n">cmmnRuntimeService</span><span class="tok-o">.</span><span class="tok-na">enablePlanItemInstance</span><span class="tok-o">(</span><span class="tok-n">planItemInstance</span><span class="tok-o">.</span><span class="tok-na">getId</span><span class="tok-o">());</span>
</td></tr></table></code></pre>
</div>
</div>
<div class="paragraph">
<p>注意, 在决定阶段或案例实例终止时，禁用状态 <em>DISABLED</em> 被视为“终端”状态. 这意味着当只有禁用的计划项实例存在，案例实例将终止.</p>
</div>
</div>
<div class="sect3">
<h4 id="_item_control_required_rule">6.4.17. Item control: Required Rule</h4>
<div class="paragraph">
<p>案例模型中的计划项可以拥有一个要求准则 <em>required rule</em>: 一个表达式被用作被外部stage 或者案例模型要求执行的一个计划项.这可以用来表明案例模型中的计划项哪些是必须的,哪些是可选的.</p>
</div>
<div class="paragraph">
<p>当未设置任何表达式, 但是启用了 <em>required rule</em> (例如, Flowable Modeler 中的复选框被选中)或者表达式为空, 默认值为true.</p>
</div>
<div class="paragraph">
<p><em>required rule</em> 与 父类 stage的 <em>autoComplete</em> 属性一起使用:
* 如果 <em>autoComplete</em> 为 <em>false</em>, 这个也是默认值, <strong>all</strong> 所有子计划项实例必须处于最终状态(完成、终止，等等), 以便由引擎完成阶段计划项实例.</p>
</div>
<div class="ulist">
<ul>
<li>
<p>如果 <em>autoComplete</em> 为 <em>true</em>, 需要所有要求准则计算为true的子计划项实例进入最终状态. 如果这没有其他处于活跃状态的子元素, 父类阶段 stage 将自动完成.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>阶段stage 拥有一个 <strong>completeable</strong> 属性来用作表示当前条件是否得到满足来完成该stage. 例如, 下面的简单阶段stage, 假设一个哨兵的条件为true, 另一个为false, 则意味着
左边的计划项实例将进入活跃状态, 右侧的将进入可用状态.</p>
</div>
<div class="imageblock" style="text-align: center">
<div class="content">
<img src="images/cmmn.completeable-stage.png" alt="cmmn.completeable stage">
</div>
</div>
<div class="paragraph">
<p>调用 <em>cmmnRuntimeService.completeStagePlanItemInstance(String stagePlanItemInstanceId)</em> 完成当前stage是不可能的(抛出异常),
因为有一个活跃状态的子元素存在. 当左侧的用户任务完成, 因为当前没有子元素活跃, <em>completeStagePlanItemInstance</em> 会被调用.
但是对于它自身, 阶段stage 不会自动完成, 因为右边的用户任务还处于可用状态.</p>
</div>
<div class="paragraph">
<p>如果上面的阶段stage 设置为 <strong>autoCompleteable</strong> (在底部有一个黑色的小矩形), 右边的计划项设置为必须的(有一个可见的感叹号标记), 表现会不同:</p>
</div>
<div class="imageblock" style="text-align: center">
<div class="content">
<img src="images/cmmn.completeable-stage02.png" alt="cmmn.completeable stage02">
</div>
</div>
<div class="ulist">
<ul>
<li>
<p>如果左边计划项实例是活跃(哨兵为true), 右边不是(哨兵为false). 这种情况下, 当左边任务完成后, stage 实例将会自动完成并且所有必须的计划项实例进入最终状态.</p>
</li>
<li>
<p>如果左右俩个任务都是活跃状态(哨兵都为true)</p>
<div class="ulist">
<ul>
<li>
<p>当左边任务完成后, 因为右边实例还处于活跃zhuangt, 当前阶段stage 不会自动完成.</p>
</li>
<li>
<p>当右边任务完成后, 因为左边必须的任务不是出于最终状态(活跃), 当前阶段 stage不会自动完成.</p>
</li>
</ul>
</div>
</li>
<li>
<p>如果左边任务不是活跃状态, 右边出于活跃. 这种情况下, 当右边任务完成后, 当前阶段stage 不会自动完成, 因为左边必须的任务不是出于一个最终状态. 只有左边任务出于活跃并且完成后, 才能完成当前阶段 stage.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>注意, 手动激活规则独立于要求准则. 例如, 以下阶段stage:</p>
</div>
<div class="imageblock" style="text-align: center">
<div class="content">
<img src="images/cmmn.completeable-stage03.png" alt="cmmn.completeable stage03">
</div>
</div>
<div class="paragraph">
<p>这里, 用户任务D是必须的, 任务B是需要手动激活.</p>
</div>
<div class="ulist">
<ul>
<li>
<p>如果D完成, 当前stage将自动完成. 因为B不是必须的并且不是活跃状态.</p>
</li>
<li>
<p>如果B也是必须的, 即使D已经完成, 还是需要手动激活B(调用 <em>cmmnRuntimeService.startPlanItemInstance(String planItemInstanceId)</em>), 然后当前阶段stage 才能自动完成.</p>
</li>
</ul>
</div>
</div>
<div class="sect3">
<h4 id="_item_control_completion_neutral_rule">6.4.18. Item control: Completion Neutral Rule</h4>
<div class="paragraph">
<p>案例模型中的计划项可以拥有一个中立完成准则 <em>completion neutral rule</em>: 一个表达式被用作对于完成父类stage 或者案例模型, 一个计划项是中立的(非必须).
这可以用来表明案例模型中的计划项哪些是必须的,哪些是可选的. 相比在某些时候使用 required rule 和 autoComplete, 会更加灵活.</p>
</div>
<div class="paragraph">
<p>注意,  中立完成准则 <em>Completion Neutral Rule</em> 不是 CMMN 1.1规范, 是 Flowable 规范的补充.</p>
</div>
<div class="paragraph">
<p>依照规范, 出于 <strong>AVAILABLE</strong> 可用状态的阶段 stage 不能被完成, 除非 <em>autoComplete</em> 属性为true, 并且计划项不是必须的. 例如, 一个计划项保持可用 <strong>AVAILABLE</strong> 状态, 哨兵条件未满足. 这意味
除非计划项被标记为非必须,并且 父类 stage 设置 <em>autoComplete</em>, 否则 父类stage 不会完成. 缺点是, 一旦一个阶段被标记为自动完成，所有子计划项目都需要为所需的规则进行配置, 这在某些用例中是冗长和繁重的工作</p>
</div>
<div class="paragraph">
<p>中立完成准则 <em>Completion Neutral Rule</em>, 与 autoComplete-required 机制相反，完成中立规则从“自底向上”工作:一个计划项目可以单独标记为中立,而无需标记任何其他计划项目.</p>
</div>
<div class="paragraph">
<p>当计划项都计算为true, <em>Required Rule</em> 优先.</p>
</div>
<div class="paragraph">
<p>To summarize:
总结:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>一个计划项设置为 <em>"completion neutral"</em>, 如果他出于活跃 <strong>AVAILABLE</strong> 状态(等待一个入口哨兵凭证), 将允许 父类stage 自动完成. 这意味着一个计划项目相对于它的父阶段完成评估是中立的.</p>
</li>
<li>
<p>在以下条件中, 一个 stage 会保持活跃 <strong>ACTIVE</strong> 状态:</p>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>它拥有至少一个出于活跃状态的计划项</p>
</li>
<li>
<p>它拥有至少一个带有 <em>requiredRule</em> , 处于可用 <strong>AVAILABLE</strong> 或者 启用 <strong>ENABLE</strong> 状态的计划项.</p>
</li>
<li>
<p>它没有被标记为 <em>autoComplete</em>, 拥有至少一个 计划项处于 启用 <strong>ENABLED</strong> 状态 (不管 必须准则 <em>requiredRule</em>)</p>
</li>
<li>
<p>它没有被标记为 <em>autoComplete</em>, 拥有至少一个 计划项处于 可用 <strong>AVAILABLE</strong> 状态并且 不是 完成中立 <em>completionNeutral</em></p>
</li>
</ol>
</div>
</li>
<li>
<p>一个阶段 将会完成:</p>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>它不包含子元素, 或者所有子计划项 都是处于 一个 终止 <em>Terminal</em> 或者 半终止 <em>Semi-terminal</em> 状态 (关闭, 完成, 禁用, 失败)</p>
</li>
<li>
<p>它没有标记为 <em>autoComplete</em> 且所有存在的子计划项处于 可用 <strong>AVAILABLE</strong> 状态, 并且是 完成中立 <em>completionNeutral</em> ,而不是必须的</p>
</li>
<li>
<p>它标记为 <em>autoComplete</em> 且所有子计划项非必须 <em>not required</em>, 处于 已启用 <strong>ENABLED</strong> 或者 可用 <strong>AVAILABLE</strong> 状态. (不管 完成中立 <em>completionNeutral</em>, 要求准则优先)</p>
</li>
</ol>
</div>
</li>
</ul>
</div>
</div>
</div>
<div class="sect2">
<h3 id="cmmn_sentry_evaluation">6.5. Sentry evaluation</h3>

</div>
<div class="sect2">
<h3 id="_哨兵计算">6.6. 哨兵计算</h3>
<div class="paragraph">
<p>在任何案例定义中, 哨兵扮演一个重要的角色. 它们提供了一个强大的声明式配置方式来激活正确的计划项或者自动停止.因此，Flowable CMMN 引擎核心逻辑最重要的部分之一是计算哨兵，以查看在一个案例实例中发生了什么状态更改。</p>
</div>
<div class="sect3">
<h4 id="_什么时候计算哨兵">6.6.1. 什么时候计算哨兵?</h4>
<div class="paragraph">
<p>在任何状态发生改变或者新事件发生,具体地说，这意味着:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>当一个案例实例启动.</p>
</li>
<li>
<p>当一个等待状态的计划项被触发继续.(比如人工任务)</p>
</li>
<li>
<p>当案例实例相关变量发生改变(新增, 修改, 或删除)</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>*当计划项实例状态改变(比如 通过 RuntimeService 停止, 一个手动计划项启动,等等)</p>
</div>
<div class="ulist">
<ul>
<li>
<p>当RuntimeService#evaluateCriteria 方法手动触发.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>伴随着状态改变的进行, 引擎会有计划地一直对当前活跃的哨兵进行计算.例如，假设一个人工任务的完成满足另一个人工任务的出口哨兵。第二个人工任务的状态更改将再次使用此新信息对所有活动哨兵进行新的评估。当最后一次评估没有发生变化时，引擎认为状态稳定，停止计算.</p>
</div>
</div>
<div class="sect3">
<h4 id="_concepts_概念">6.6.2. Concepts 概念</h4>
<div class="paragraph">
<p>哨兵包含两部分:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>与其他计划项生命周期相关的一个或者多个 <em>onParts</em></p>
</li>
<li>
<p>零个或者一个 if条件</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>注意以下案例定义:</p>
</div>
<div class="imageblock" style="text-align: center">
<div class="content">
<img src="images/cmmn.sentry-eval-01.png" alt="cmmn.sentry eval 01">
</div>
</div>
<div class="paragraph">
<p>假设(上图中未展示出来)</p>
</div>
<div class="ulist">
<ul>
<li>
<p>任务C的入口哨兵监听任务 A和B的完成 <em>complete</em> 事件</p>
</li>
<li>
<p>出口哨兵监听 用户事件监听器的发生 <em>occur</em> 事件 <em>'Stop  C'</em></p>
</li>
<li>
<p>入口哨兵有个表达式设置为 <em>${var:eq(myVar, <em>hello world</em>)}</em></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>在这个简单例子中, 入口哨兵 有两个 onParts 和 一个 ifPart. 退出哨兵只有一个 onPart.</p>
</div>
<div class="paragraph">
<p>当案例实例启动后, 人工任务A和B被创建(他们没有入口哨兵), 很快进入活跃 <em>active</em> 状态. C因为入口哨兵条件不满足, 处于可用 <em>available</em> 状态. 用户事件监听器“Stop C”从一开始就可用，因此可以被触发.</p>
</div>
<div class="paragraph">
<p>当任务A和B都完成并且变量 <em>myVar</em> 设置为 <em>'hello world'</em>, 入口哨兵条件满足并且触发. C后面的计划项实例被移动到active状态, 同时,人工任务C被创建(例如，现在可以通过 <em>TaskService</em> 查询它).
当&#8217;Stop C&#8217;被触发(通过 <em>CmmnRuntimeService#completeUserEventListenerInstance</em> 方法),出口哨兵条件满足, C被中止.</p>
</div>
<div class="paragraph">
<p>如果 <em>'Stop C'</em> 在 C变成活跃 <em>active</em> 状态前被触发, 它的计划项实例将被中止, 出口哨兵也将不再监听任何.</p>
</div>
</div>
<div class="sect3">
<h4 id="_默认行为">6.6.3. 默认行为</h4>
<div class="paragraph">
<p>当一个案例实例启动后</p>
</div>
<div class="listingblock">
<div class="content">
<pre>CaseInstance caseInstance = cmmnRuntimeService.createCaseInstanceBuilder()
    .caseDefinitionKey("myCase")
    .start();</pre>
</div>
</div>
<div class="paragraph">
<p>入口哨兵条件会立即被计算, 因此当案例实例启动会有一个有序的循环计算.</p>
</div>
<div class="paragraph">
<p>注意, 如果使用像_${myVar == <em>hello world</em>}_ 条件表达式不会起作用. 引擎会抛出 PropertyNotFound 异常, 因为它不知道 变量myVar.</p>
</div>
<div class="paragraph">
<p>为了解决上述问题:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>在启动时传一个 <em>myVar</em> 变量</p>
</li>
<li>
<p>做一个非空校验, 类似 <em>${planItemInstance.getVariable(<em>myVar</em>) != null &amp;&amp; planItemInstance.getVariable(<em>myVar</em>) == <em>hello world</em>}</em></p>
</li>
<li>
<p>或者更简单地, 使用 表达式函数 <a href="#cmmnExpressionsFunctions">expression functions</a> , 类似 <em>${var:eq(myVar, <em>hello world</em>)}</em> 考虑到变量可能不存在的情况</p>
</li>
</ul>
</div>
<div class="paragraph">
<p><strong>默认的计算逻辑有“缓存”，这意味着当哨兵的一部分条件得到满足时，引擎将存储并在随后的计算中并"记住"这个值.</strong></p>
</div>
<div class="paragraph">
<p>这意味着, 从某一部分(onPart或ifPart of the sentry)得到满足的那一刻起, 该特定部分在下一次评估中不再被评估, 而是被认为是正确的.</p>
</div>
<div class="paragraph">
<p>在上述例子中,这是任务A相对于B通常会在另一个时间点完成。例如,如果任务A完成后,任务C哨兵的一部分说“我监听任务A完成“得到满意,这事实上为将来被缓存。如果现在B完成了,
它也被存储。如果 <em>myVar</em> 变量得到正确的值，ifPart也会触发，整个岗兵和任务C也会被激活。当然，也可以先满足变量值，然后再满足任务. 关键是这并不重要,因为引擎会保存之前满足的部分条件</p>
</div>
<div class="paragraph">
<p>这是流程引擎的默认行为, 可以设置 哨兵 <em>triggerMode</em> 为 <strong>default</strong> 覆盖. 在Flowable Modeler 中增加一个新计划项时自动被设置.
当不设置(例如从另一个工具导入模型)时, <em>triggerMode</em> 默认是 <strong>default</strong>.</p>
</div>
</div>
<div class="sect3">
<h4 id="_trigger_mode_onevent_事件触发模式">6.6.4. Trigger mode "onEvent" 事件触发模式</h4>
<div class="paragraph">
<p>默认行为(查看前面部分)是保存先前被满足的部分. 这是最常用和最安全的方法(也是对哨兵进行推理时通常期望的方法).</p>
</div>
<div class="paragraph">
<p>另一种哨兵触发模式称为“onEvent”。在这种模式下，引擎将有关于哨兵的部分保存，不会保存*not remember*过去满足的任何部分。这在高级用例中有时是需要的。以下面的例子为例:</p>
</div>
<div class="imageblock" style="text-align: center">
<div class="content">
<img src="images/cmmn.sentry-eval-02.png" alt="cmmn.sentry eval 02">
</div>
</div>
<div class="paragraph">
<p>这里, 案例模型是一个有三个子阶段stage. 所有子阶段都可以重复. 子阶段B和C有一个入口哨兵用来等待阶段A的完成. 同理(未展示出来), 两个哨兵都有一个变量的依赖条件.</p>
</div>
<div class="paragraph">
<p>在高级用例中, 可能想要或需要哨兵部分(特别是包含条件的ifPart)仅在相关计划项的生命周期事件发生时才进行计算。在本例中，正是Stage A的完成事件。
对于这些用例，可以将哨兵的 <em>triggerMode</em> 设置为onEvent。顾名思义，这意味着哨兵的计算只在引用事件发生时发生，而不考虑对过去事件的缓存。</p>
</div>
<div class="paragraph">
<p>具体地说, 在这里的例子中，只有在阶段A完成时(而不是在其他时刻)才会计算入口哨兵的情况. 这与一般的评价规则有很大的不同.
在这个特殊的例子中,它确实使管理变量变得更容易, 因为条件只在一个精确的时刻进行评估，并且不需要担心由于某个变量在某个时间点的具体值而触发某个哨兵的部分.
特别是在本例中，所有子阶段都在重复，这需要做很多工作。这是一种功能强大的机制，但对于高级建模人员来说，这意味着他们对案例模型和这种 <em>triggerMode</em> 的语义具有内在的知识.</p>
</div>
<div class="paragraph">
<p>注意，在计算哨兵时，引擎认为所有的事件都是同时发生的。以下面的案例定义为例:</p>
</div>
<div class="imageblock" style="text-align: center">
<div class="content">
<img src="images/cmmn.sentry-eval-03.png" alt="cmmn.sentry eval 03">
</div>
</div>
<div class="paragraph">
<p>假设所有的哨兵使用 <em>triggerMode onEvent</em> 的设置. 如果任务A完成, 这会使任务B退出, 任务C也会退出. 所以,即使有两种截然不同的生命周期事件(A被完成和B被退出),人们可能会认为 <em>onEvent</em> 的字面意思是,
有两种截然不同的计算发生, 而另一部分C的退出哨兵的缓存被忘记.引擎足够聪明地看到,他们是相同的计算周期且任务C也将退出.</p>
</div>
<div class="paragraph">
<p>从技术上讲: <em>onEvent</em> 哨兵存在缓存，更具体地说，是用于在同一API调用(或事务，简单地说)期间发生的计算.</p>
</div>
<div class="paragraph">
<p><strong>Important</strong> onEvent是一种功能强大的机制，只有当很好地理解语义时才应该使用它。如果不仔细检查用例，可能会创建由于没有正确的配置哨兵而卡住的用例模型。</p>
</div>
<div class="paragraph">
<p>(例如, 假设一个哨兵有一个onPart监听一个计划项的完成情况，还有一个带有条件的ifPart。如果计划项完成, 从而触发onPart, 但是由于某种原因缺少了条件中使用的变量…… ifPart将永远不会触发，case实例可能会陷在一个不想要的状态).</p>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_体系结构">7. 体系结构</h2>
<div class="sectionbody">
<div id="architecture" class="paragraph">
<p>本章从高层次的角度简要介绍了CMMN引擎的内部结构。当然，由于CMMN引擎代码是开源的，因此可以通过深入了解源代码来找到实际的实现细节。</p>
</div>
<div class="paragraph">
<p>如下图所示，CMMN引擎是Flowable引擎生态系统的一部分，其设计方式与其他引擎类似。CMMN引擎基于CMMN特定服务和共享服务构建：任务，变量，身份和作业服务，它们独立于引擎。 在较低级别，实体和数据管理器层负责低级别持久性，相当于其他引擎。</p>
</div>
<div class="imageblock" style="text-align: center">
<div class="content">
<img src="images/cmmn.architecture.png" alt="cmmn.architecture">
</div>
</div>
<div class="paragraph">
<p>通过使用共享服务，这也意味着例如来自BPMN和CMMN引擎的任务将最终结合在一起，并且可以通过相同的API查询和管理。类似地，对于异步执行器：定时器和异步作业使用相同的逻辑，甚至可以由中央执行器管理。</p>
</div>
<div class="paragraph">
<p>共享服务架构有第二个好处。通常情况下，当引擎一起使用时，引擎将尽可能共享资源，数据库事务将跨越多个引擎操作的调用，查找缓存是常见的，并且持久性是独立于引擎本身处理的。</p>
</div>
<div class="paragraph">
<p>CMMN引擎的设计原理与Flowable项目的其他引擎相同。下图给出了使用CMMN引擎时涉及的不同组件的高级概述：</p>
</div>
<div class="imageblock" style="text-align: center">
<div class="content">
<img src="images/cmmn.api-call-flow.png" alt="cmmn.api call flow">
</div>
</div>
<div class="paragraph">
<p>从高层次来看：</p>
</div>
<div class="paragraph">
<p>一个<strong>CmmnEngine</strong>实例是从<strong>CmmnEngineConfiguration</strong>创建的。可以从配置文件或以编程方式创建配置实例。</p>
</div>
<div class="paragraph">
<p><em>CmmnEngine</em>以服务的形式提供对Flowable CMMN API的访问：<strong>CmmnRepositoryService</strong>、<strong>CmmnRuntimeService</strong>、<strong>CmmnTaskService</strong>、<strong>CmmnHistoryService</strong>、<strong>CmmnManagementService</strong>。服务的命名及其职责与其他引擎类似。</p>
</div>
<div class="paragraph">
<p>每个API方法都转换为<strong>Command</strong>实例，此<em>Command</em>实例传递给<strong>CommandExecutor</strong>，其中包括使一些<strong>CommandInterceptors</strong>。这些拦截器具有各种职责，包括处理事务。</p>
</div>
<div class="paragraph">
<p>最终，<em>Command</em>通常（除非是纯数据修改<em>Command</em>）在<strong>CmmnEngineAgenda</strong>上计划<strong>CmmnOperation</strong>。</p>
</div>
<div class="paragraph">
<p>操作将从<em>CmmnEngineAgenda</em>中获取，直到不再进行操作为止。通常，操作将计划新操作作为其逻辑的一部分。</p>
</div>
<div class="paragraph">
<p>操作中的逻辑将调用较低级别的服务和/或实体管理器。</p>
</div>
<div class="paragraph">
<p>BPMN和CMMN引擎之间的一个很大区别是BPMN引擎通常是“本地的”：引擎查看当前状态，检查进程中的前面的信息并继续（当然这是一个简化，这有很多不适用的操作，但从概念上做出区分，这是正确的）。CMMN引擎的工作方式不同：在CMMN中，数据起着重要作用，数据的更改可以在案例定义中的各个位置触发很多事情。因此，只要发生更改，就会经常计划和执行<strong>EvaluateCriteriaOperation</strong>。当检测到重复或无用的评估时，引擎会对这些评估进行优化。</p>
</div>
<div class="paragraph">
<p>CMMN引擎工作的核心是计划项目实例的概念，表示当前在案例中存在哪些<strong>计划实例项</strong>以及它们具有哪种状态。与BPMN完全不同，CMMN为<em>计划项</em>定义了严格的状态<em>生命周期</em>。这在<em>CmmnRuntimeService</em>方法，查询API和<strong>PlanItemInstance</strong>对象的数据字段部分中表示。</p>
</div>
<div class="paragraph">
<p>可以通过将日志记录设置为议程包的调试来检查议程的工作，操作以及如何处理计划项实例。例如，使用log4j时：</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code>log4j.logger.org.flowable.cmmn.engine.impl.agenda=DEBUG</code></pre>
</div>
</div>
<div class="paragraph">
<p>这样记录的信息如下：</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code>Planned [Init Plan Model] initializing plan model for case instance bfaf0e64-eaf4-11e7-b9d0-acde48001122
Planned [Change PlanItem state] Task A (id: planItemTaskA), new state: [available] with transition [create]
Planned [Change PlanItem state] PlanItem Milestone One (id: planItemMileStoneOne), new state: [available] with transition [create]
Planned [Change PlanItem state] Task B (id: planItemTaskB), new state: [available] with transition [create]
Planned [Change PlanItem state] PlanItem Milestone Two (id: planItemMileStoneTwo), new state: [available] with transition [create]
Planned [Evaluate Criteria] case instance bfaf0e64-eaf4-11e7-b9d0-acde48001122
Planned [Evaluate Criteria] case instance bfaf0e64-eaf4-11e7-b9d0-acde48001122 with transition 'create' having fired for plan item planItemTaskA (Task A)
Planned [Evaluate Criteria] case instance bfaf0e64-eaf4-11e7-b9d0-acde48001122 with transition 'create' having fired for plan item planItemMileStoneOne (PlanItem Milestone One)
Planned [Evaluate Criteria] case instance bfaf0e64-eaf4-11e7-b9d0-acde48001122 with transition 'create' having fired for plan item planItemTaskB (Task B)
Planned [Evaluate Criteria] case instance bfaf0e64-eaf4-11e7-b9d0-acde48001122 with transition 'create' having fired for plan item planItemMileStoneTwo (PlanItem Milestone Two)
Planned [Activate PlanItem] Task A (planItemTaskA)
Planned [Change PlanItem state] Task A (id: planItemTaskA), new state: [active] with transition [start]
Planned [Evaluate Criteria] case instance bfaf0e64-eaf4-11e7-b9d0-acde48001122 with transition 'start' having fired for plan item planItemTaskA (Task A)
Planned [Change PlanItem state] Task A (id: planItemTaskA), new state: [completed] with transition [complete]
Planned [Evaluate Criteria] case instance bfaf0e64-eaf4-11e7-b9d0-acde48001122 with transition 'complete' having fired for plan item planItemTaskA (Task A)
Planned [Activate PlanItem] PlanItem Milestone One (planItemMileStoneOne)
Planned [Change PlanItem state] PlanItem Milestone One (id: planItemMileStoneOne), new state: [active] with transition [start]
Planned [Evaluate Criteria] case instance bfaf0e64-eaf4-11e7-b9d0-acde48001122 with transition 'start' having fired for plan item planItemMileStoneOne (PlanItem Milestone One)
Planned [Change PlanItem state] PlanItem Milestone One (id: planItemMileStoneOne), new state: [completed] with transition [occur]
Planned [Evaluate Criteria] case instance bfaf0e64-eaf4-11e7-b9d0-acde48001122 with transition 'occur' having fired for plan item planItemMileStoneOne (PlanItem Milestone One)
Planned [Activate PlanItem] Task B (planItemTaskB)
Planned [Change PlanItem state] Task B (id: planItemTaskB), new state: [active] with transition [start]
Planned [Evaluate Criteria] case instance bfaf0e64-eaf4-11e7-b9d0-acde48001122 with transition 'start' having fired for plan item planItemTaskB (Task B)
Planned [Change PlanItem state] Task B (id: planItemTaskB), new state: [completed] with transition [complete]
Planned [Evaluate Criteria] case instance bfaf0e64-eaf4-11e7-b9d0-acde48001122 with transition 'complete' having fired for plan item planItemTaskB (Task B)
Planned [Activate PlanItem] PlanItem Milestone Two (planItemMileStoneTwo)
Planned [Change PlanItem state] PlanItem Milestone Two (id: planItemMileStoneTwo), new state: [active] with transition [start]
Planned [Evaluate Criteria] case instance bfaf0e64-eaf4-11e7-b9d0-acde48001122 with transition 'start' having fired for plan item planItemMileStoneTwo (PlanItem Milestone Two)
Planned [Change PlanItem state] PlanItem Milestone Two (id: planItemMileStoneTwo), new state: [completed] with transition [occur]
Planned [Evaluate Criteria] case instance bfaf0e64-eaf4-11e7-b9d0-acde48001122 with transition 'occur' having fired for plan item planItemMileStoneTwo (PlanItem Milestone Two)
Planned [Evaluate Criteria] case instance bfaf0e64-eaf4-11e7-b9d0-acde48001122
No active plan items found for plan model, completing case instance
Planned [Complete case instance] case instance bfaf0e64-eaf4-11e7-b9d0-acde48001122</code></pre>
</div>
</div>
</div>
</div>
</div>
</body>
</html>